It's as easy as:
The example is comprised of two parts: a server and a client. Whenever a client connects, it starts receiving from the server a periodic sequence number. A new sequence is started for each new client, so the server has to keep track of them individually. That's where the "I need to send a message to a particular client" comes into play. The code is very simple to understand. Let's see it.
The server starts listening on port 8000 for incoming connections. When one arrives, it adds that new client to a map so it can keep track of its sequence number. It also listens for that client's disconnect event, when it'll remove it from the map.
Each and every second, a timer is fired. When it does, the server walks through the map and sends a message to every client with its current sequence number. It then increments it and stores the number back in the map. That's all that is to it. Easy peasy.
The client part is even simpler. It just connects to the server and listens for the seq-num message, printing it to the console every time it arrives.
io = require("socket.io-client"),
ioClient = io.connect("http://localhost:8000");
ioClient.on("seq-num", (msg) => console.info(msg));
npm install socket.io
npm install socket.io-client
Open other terminal windows and spawn as many clients as you want by running:
Is port 8000 the norm for socketio in production?
Sorry I took so long to reply back, @ingo. 8000 is a common port used by the Node community when testing both websockets or HTTP servers (3000 is also common). Of course you could use it in production, but I'm not sure how common that is... anyway, you can just use any port really, as long as your gateways/load balancers/etc are prepared for that.
Thanks a lot man! =)