Targeting the specific user

Adding the user to the room is great, but if we can't target them specifically and others in that room, it's not very useful. Well it turns out we have a couple of ways to do just that. In order to illustrate how we're going to target specific users, let's look at all the ways we've emitted events on the server.

We've used io.emit. This emits it to every single connected user, and that's currently what we're doing for things like createMessage. A new message comes in and we emit it to everyone connected. Next up, we have used socket.broadcast.emit. We use that with newMessage and as we know this sends the message to everyone connected to the socket server except for the current user. The last one we used inside ...

Get Advanced Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.