Updating the event listener

It's also pretty simple on the server; we're going to add a second argument to our callback argument list. The first one is still going to be the data that was emitted, the second one though is going to be a function that we're going to refer to as callback. And we can call it anywhere in socket.on to acknowledge that we got the request:

socket.on('createMessage', (message, callback) => {  console.log('createMessage', message);  io.emit('newMessage', generateMessage(message.from, message.text));  callback();

When we call this function, like we're going to call it right here, it is in turn going to send an event back to the frontend and it is going to call the function as we have it in event emitter in index.js.

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.