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 a listener, so we'll not provide a callback function. What we want to do is specify the data. Now, by default, we don't have to specify any data; maybe we just want to emit newEmail without anything, letting the browser know that something happened. If we do this, over inside the browser we can refresh the app, and we get New email, as shown in the following screenshot:

The event is still happening even though we're not sending across any custom data. If you do want to send custom data, which is most likely the case, that's super easy. All you have to do is provide a second argument for newEmail. Now, you could provide an argument of three, or true, or anything else, but you usually want to send multiple pieces of data across, so an object is going to be your second argument:
socket.emit('newEmail', {
});
This is going to let you specify anything you like. In our case, we might specify who the email is from by specifying a from attribute; for instance, it's from mike@example.com. Maybe we also have the text attribute for the email, Hey. What is going on, and we might have other attributes as well. For example, createdAt could be a timestamp of when the server got the email, as shown here:
socket.emit('newEmail', {
from: 'mike@example.com',
text: 'Hey. What is going on.',
createdAt: 123
});
The data shown in the preceding code block will get sent along with the newEmail event from the server to the client. Now, go ahead and save server.js, and inside our client-side JavaScript index.js file, we can go ahead and do something with that data. The data that's emitted with your event is provided as the first argument to your callback function. As shown in the following code, we have our callback function for newEmail, which means we can name the first argument email and do whatever we want with it:
socket.on('newEmail', function (email) {
console.log('New email');
});
We might be appending it to a list of emails in a real web app, but for our purposes, all we'll do right now is provide it as the second argument to console.log, rendering it to the screen:
socket.on('newEmail', function (email) {
console.log('New email', email);
});
With this in place, we can now test that everything is working as expected.