The newMessage event

To get started with this one, we'll add our event listener inside index.js. We have the old event listener for newEmail. I'm going to go ahead and remove that, and we'll call socket.on to listen to the new event, newMessage. The newMessage event is going to require a callback:

socket.on('newMessage', function () {});

For the moment, we'll use console.log to print the message to the console, but later, we'll be taking this message and adding it to the browser so a user can actually see it on the screen. Now, we are going to get the message data. I'll create an argument called message for the moment, and we can go ahead and simply log it to the screen using console.log, printing the name of the event so it's easy to track ...

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.