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 ...