Emitting event to the individual room

First up, we want to emit to just a specific room and we know we can tack on a to call right in the io.emit line to get that done, passing in the room name, with access to that on the user object user.room. Now that we're emitting to just the individual room, we also do want to change the name we use. Instead of message.from, we're going to access the name on the user object, user.name, and there we go:

io.to(user.room).emit('newMessage', generateMessage(user.name, message.text)); 

Now we have a much better system for sending those text messages. I'm going to refresh my first tab and my second tab, and we're going to send some text messages around. I'm going to send the number 1 from my second tab and ...

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.