How to send a message to a particular client with socket.io

You can use socket.io rooms. From the client side emit an event (“join” in this case, can be anything) with any unique identifier (email, id). Client Side: var socket = io.connect(‘http://localhost’); socket.emit(‘join’, {email: user1@example.com}); Now, from the server side use that information to create an unique room for that user Server Side: var io = … Read more

What is an example of the simplest possible Socket.io example?

Edit: I feel it’s better for anyone to consult the excellent chat example on the Socket.IO getting started page. The API has been quite simplified since I provided this answer. That being said, here is the original answer updated small-small for the newer API. index.html <!doctype html> <html> <head> <script src=”https://stackoverflow.com/socket.io/socket.io.js”></script> <script> var socket = … Read more

WebSockets and Apache proxy: how to configure mod_proxy_wstunnel?

I finally managed to do it, thanks to this topic. TODO: 1) Have Apache 2.4 installed (doesn’t work with 2.2), and do: a2enmod proxy a2enmod proxy_http a2enmod proxy_wstunnel 2) Have nodejs running on port 3001 3) Do this in the Apache config <VirtualHost *:80> ServerName example.com RewriteEngine On RewriteCond %{REQUEST_URI} ^/socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket … Read more

Get the client’s IP address in socket.io

Okay, as of 0.7.7 this is available, but not in the manner that lubar describes. I ended up needing to parse through some commit logs on git hub to figure this one out, but the following code does actually work for me now: var io = require(‘socket.io’).listen(server); io.sockets.on(‘connection’, function (socket) { var address = socket.handshake.address; … Read more