R
R
Rince19692015-01-02 19:20:25
Sessions
Rince1969, 2015-01-02 19:20:25

How to connect socket.io, express, session?

There are several static html files that need to communicate with the nodejs server in real time. In particular, the server must receive the session id of the users. I found several ready-made solutions, but with more complex functionality (using databases, template engines, debuggers, etc.), which complicates the ability to deal with them. The code of the server part is of interest: the return of statics, the exchange of data between the client and the server, interaction with sessions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimon, 2016-01-11
@Glimor

app.js

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.sockets.on('connection', function(client) {
  console.log("Client connected...");

  client.on('question', function(question) {
    if(!client.question_asked) {
      client.question_asked = true;
      client.broadcast.emit('question', question);
    }
  });
});

server.listen(8080);

Example taken from campus.codeschool.com lessons on node

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question