Using PHP with Socket.io

It may be a little late for this question to be answered, but here is what I found. I don’t want to debate on the fact that nodes does that better than php or not, this is not the point. The solution is : I haven’t found any implementation of socket.io for PHP. But there … Read more

Authenticating socket io connections using JWT

It doesn’t matter if the token was created on another server. You can still verify it if you have the right secret key and algorithm. Implementation with jsonwebtoken module client const {token} = sessionStorage; const socket = io.connect(‘http://localhost:3000’, { query: {token} }); Server const io = require(‘socket.io’)(); const jwt = require(‘jsonwebtoken’); io.use(function(socket, next){ if (socket.handshake.query … Read more

socket.emit() vs. socket.send()

With socket.emit you can register custom event like that: server: var io = require(‘socket.io’).listen(80); io.sockets.on(‘connection’, function (socket) { socket.emit(‘news’, { hello: ‘world’ }); socket.on(‘my other event’, function (data) { console.log(data); }); }); client: var socket = io.connect(‘http://localhost’); socket.on(‘news’, function (data) { console.log(data); socket.emit(‘my other event’, { my: ‘data’ }); }); Socket.send does the same, but … Read more

Maximum concurrent Socket.IO connections

This article may help you along the way: http://drewww.github.io/socket.io-benchmarking/ I wondered the same question, so I ended up writing a small test (using XHR-polling) to see when the connections started to fail (or fall behind). I found (in my case) that the sockets started acting up at around 1400-1800 concurrent connections. This is a short … Read more

socket.io rooms or namespacing?

This is what namespaces and rooms have in common (socket.io v0.9.8 – please note that v1.0 involved a complete rewrite, so things might have changed): Both namespaces (io.of(‘/nsp’)) and rooms (socket.join(‘room’)) are created on the server side Multiple namespaces and multiple rooms share the same (WebSocket) connection The server will transmit messages over the wire … Read more

node.js, socket.io with SSL

Use a secure URL for your initial connection, i.e. instead of “http://” use “https://”. If the WebSocket transport is chosen, then Socket.IO should automatically use “wss://” (SSL) for the WebSocket connection too. Update: You can also try creating the connection using the ‘secure’ option: var socket = io.connect(‘https://localhost’, {secure: true});

How do I get a list of connected sockets/clients with Socket.IO?

In Socket.IO 0.7 you have a clients method on the namespaces. This returns an array of all connected sockets. API for no namespace: var clients = io.sockets.clients(); var clients = io.sockets.clients(‘room’); // all users from room `room` For a namespace var clients = io.of(‘/chat’).clients(); var clients = io.of(‘/chat’).clients(‘room’); // all users from room `room` Note: … Read more

node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

The error is pretty clear, you need to specify an absolute (instead of relative) path and/or set root in the config object for res.sendFile(). Examples: // assuming index.html is in the same directory as this script res.sendFile(__dirname + ‘/index.html’); or specify a root (which is used as the base path for the first argument to … Read more