The emit method

Inside server.js, what we want to do is call a method on socket. The socket method has a method called emit, which we'll use on both the client and the server to emit events:

io.on('connection', (socket) => {  console.log('New user connected');    socket.emit('');});

The emit method is really similar to the listeners; although, instead of listening to an event, we are creating the event. The first argument is the same. It's going to be the name of the event you want to emit. In this case, we have to match it exactly as we specified in index.js, newEmail. Now, as shown in the following code, we'll provide newEmail:

io.on('connection', (socket) => {  console.log('New user connected');    socket.emit('newEmail');});

Now, this is not ...

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.