How to connect two node.js servers with websockets?

For future people: Here is 2 very simple Node.js apps that use socket.io to connect, send and receive messages between each other. Required package is: npm install socket.io Node-App-1 server.js: var io = require(‘socket.io’).listen(3000); io.on(‘connection’, function (socket) { console.log(‘connected:’, socket.client.id); socket.on(‘serverEvent’, function (data) { console.log(‘new message from client:’, data); }); setInterval(function () { socket.emit(‘clientEvent’, Math.random()); … Read more

How to unsubscribe from a socket.io subscription?

From looking at the source of socket.io.js (couldn’t find it in documentation anywhere), I found these two functions: removeListener = function(name, fn) removeAllListeners = function(name) I used removeAllListeners successfully in my app; you should be able to choose from these: socket.removeListener(“news”, cbProxy); socket.removeAllListeners(“news”); Also, I don’t think your solution of cbProxy = _blank would actually … Read more

Unit testing Node.js and WebSockets (Socket.io)

After further poking and prodding, I found some incredibly useful information. In the author’s example, he points out the critical step of establishing socket listeners in the before hooks. This example works: Assuming a server is listening for socket connections at localhost:3001, of course var io = require(‘socket.io-client’) , assert = require(‘assert’) , expect = … Read more

Scaling Socket.IO to multiple Node.js processes using cluster

Edit: In Socket.IO 1.0+, rather than setting a store with multiple Redis clients, a simpler Redis adapter module can now be used. var io = require(‘socket.io’)(3000); var redis = require(‘socket.io-redis’); io.adapter(redis({ host: ‘localhost’, port: 6379 })); The example shown below would look more like this: var cluster = require(‘cluster’); var os = require(‘os’); if (cluster.isMaster) … Read more

io.emit vs socket.emit

Here’s a supplementary documentation for reference : socket.emit(‘message’, “this is a test”); //sending to sender-client only socket.broadcast.emit(‘message’, “this is a test”); //sending to all clients except sender socket.broadcast.to(‘game’).emit(‘message’, ‘nice game’); //sending to all clients in ‘game’ room(channel) except sender socket.to(‘game’).emit(‘message’, ‘enjoy the game’); //sending to sender client, only if they are in ‘game’ room(channel) socket.broadcast.to(socketid).emit(‘message’, … Read more

how does socket.io work? [closed]

For debugging, you might want to try out Theseus. Here is a short overview of the socket.io SPEC: Socket.IO aims to bring a WebSocket-like API to many browsers and devices, with some specific features to help with the creation of real-world realtime applications and games. Multiple transport support (old user agents, mobile browsers, etc). Multiple … Read more

Pusher vs Pubnub vs open source Socket.io / SignalR.net / Faye / jWebSocket [closed]

Faye using Node.js was very easy to set up for me and initially performed very well in testing. However even though the load on my App is only about 10 requests per second with around 3000 open connections – when I switched it to live node.js cpu usage was pinned at 100% (1 core out … Read more