Socket.io Client: respond to all events with one handler?

Updated solution for socket.io-client 1.3.7 var onevent = socket.onevent; socket.onevent = function (packet) { var args = packet.data || []; onevent.call (this, packet); // original call packet.data = [“*”].concat(args); onevent.call(this, packet); // additional call to catch-all }; Use like this: socket.on(“*”,function(event,data) { console.log(event); console.log(data); }); None of the answers worked for me, though the one … Read more

Websocket transport reliability (Socket.io data loss during reconnection)

Others have hinted at this in other answers and comments, but the root problem is that Socket.IO is just a delivery mechanism, and you cannot depend on it alone for reliable delivery. The only person who knows for sure that a message has been successfully delivered to the client is the client itself. For this … Read more

Send additional data on socket connection

You should send your data either in connect or on create: var s = io(‘http://216.157.91.131:8080/’, { query: “foo=bar” }); s.connect(); var c = io.connect(‘http://216.157.91.131:8080/’, { query: “foo=bar” }); With the new version of socket.io, on server is where the things have been changed: var io = require(‘socket.io’)(server); io.use(function(socket, next) { var handshakeData = socket.request; console.log(“middleware:”, … Read more

Using socket.io in Express 4 and express-generator’s /bin/www

Here is how you can add Socket.io to a newly generated Express-Generator application: Create a file that will contain your socket.io logic, for example socketapi.js: socketapi.js: const io = require( “socket.io” )(); const socketapi = { io: io }; // Add your socket.io logic here! io.on( “connection”, function( socket ) { console.log( “A user connected” … Read more

socket.io and session?

This won’t work for sockets going over the flashsocket transport (it doesn’t send the server the needed cookies) but it reliably works for everything else. I just disable the flashsocket transport in my code. To make it work, in the express/connect side, I explicitly define the session store so I can use it inside socket: … Read more

How to share sessions with Socket.IO 1.x and Express 4.x?

The solution is surprisingly simple. It’s just not very well documented. It is possible to use the express session middleware as a Socket.IO middleware too with a small adapter like this: sio.use(function(socket, next) { sessionMiddleware(socket.request, socket.request.res, next); }); Here’s a full example with express 4.x, Socket.IO 1.x and Redis: var express = require(“express”); var Server … Read more

Faye vs. Socket.IO (and Juggernaut)

Disclosure: I am the author of Faye. Regarding Faye, everything you’ve said is true. Faye implements most of Bayeux, the only thing missing right now is service channels, which I’ve yet to be convinced of the usefulness of. In particular Faye is designed to be compatible with the CometD reference implementation of Bayeux, which has … Read more

Socket.IO handling disconnect event

Ok, instead of identifying players by name track with sockets through which they have connected. You can have a implementation like Server var allClients = []; io.sockets.on(‘connection’, function(socket) { allClients.push(socket); socket.on(‘disconnect’, function() { console.log(‘Got disconnect!’); var i = allClients.indexOf(socket); allClients.splice(i, 1); }); }); Hope this will help you to think in another way