Emitting two events when a user connects

With broadcasting in place, let's get into the final way we emit messages. We'll emit two events in socket.io, right when a user connects. Now, we'll not actually use broadcasting in this context, so we'll comment the broadcast object out and uncomment our old code. It should look like this:

socket.on('createMessage', (message) => {  console.log('createMessage', message);  io.emit('newMessage', {    from: message.from,    text: message.text,    createdAt: new Date().getTime()  });  // socket.broadcast.emit('newMessage', {  // from: message.from,  // text: message.text,  // createdAt: new Date().getTime()  //});});

You'll first call socket.emit to emit a message to the user who joins. Your message should come from the ...

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.