Wiring up the createMessage listener for all users

To get started, we'll update the createMessage listener. Currently, all we do is log the data to the screen. But here, instead of just logging it, we actually want to emit a new event, a newMessage event, to everybody, so each connected user gets the message that was sent from a specific user. In order to get that done, we'll call a method on io, which will be io.emit:

socket.on('createMessage', (message) => {  console.log('createMessage', message);  io.emit});

Socket.emit emits an event to a single connection, whereas io.emit emits an event to every single connection. Here, we are going to emit the newMessage event, specifying it as our first argument. The second argument, as with socket.emit ...

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.