How to use ExpressJS and Socket.io on a same port?

It’s described on the socket.io github page (as @Golo stated in your comment): var app = express() , server = require(‘http’).createServer(app) , io = io.listen(server); server.listen(80); This works, I have it running. Probably what Golo have forgotten is to change the listen from app.listen(80) to server.listen(80). I’ve struggled with this too until I realised my … Read more

Set Client-Side Accessible Cookie In Express

Figured it out! By default Express sets the option httpOnly to true. This means that your cookies cannot be accessed by the client-side Javascript. In order to correctly set cookies accessible on the client just use a snippet like the following: res.cookie(‘rememberme’, ‘yes’, { maxAge: 900000, httpOnly: false}); I’ve also noticed that if you call … Read more

Use socket.io inside a express routes file

There is a better way to do this now with Express 4.0. You can use app.set() to store a reference to the io object. Base configuration: var app = require(‘express’)(); var server = app.listen(process.env.PORT || 3000); var io = require(‘socket.io’)(server); // next line is the money app.set(‘socketio’, io); Inside route or middleware: exports.foo = function(req,res){ … Read more

What do these numbers mean in socket.io payload?

I know you asked a while ago, but the information remains for those who are researching. I did an analysis with reverse engineering in version 2.3.0 (socket.io) and 3.4.2 (engine.io) and got the following: The first number is the type of communication for engine.io, using the enumerator: Key Value 0 “open” 1 “close” 2 “ping” … Read more

socket.io determine if a user is online or offline

If your clients have specific user IDs they need to send them to socket.io server. E.g. on client side you can do // Browser (front-end) <script> const socket = io(); socket.emit(‘login’,{userId:’YourUserID’}); </script> And on server you will put something like // server (back-end) const users = {}; io.on(‘connection’, function(socket){ console.log(‘a user connected’); socket.on(‘login’, function(data){ console.log(‘a … Read more

Socket.io how to send JavaScript object

You actually need to emit an event instead: socket.emit(‘yourEvent’, myObject); If you use .send(), you are simply sending the string representation of your object, which is where the problem is occurring. Note that you can use .send(), but you would have to JSON-encode the object first, and decode it on reception. Unless you have a … Read more

Engine.io or SockJS, which one to choose?

Have you looked at Primus? It offers the cookie requirements you mention, it supports all of the major ‘real-time’/websocket libraries available and is a pretty active project. To me it also sounds like vendor lock-in could be a concern for you and Primus would address that. The fact that it uses a plugin system should … Read more

How to get room’s clients list in socket.io 1.0

Consider this rather more complete answer linked in a comment above on the question: https://stackoverflow.com/a/24425207/1449799 The clients in a room can be found at io.nsps[yourNamespace].adapter.rooms[roomName] This is an associative array with keys that are socket ids. In our case, we wanted to know the number of clients in a room, so we did Object.keys(io.nsps[yourNamespace].adapter.rooms[roomName]).length In … Read more