To kick things off, we'll fill out the first call. This is a call to socket.emit, and this call will be responsible for greeting the individual users:
// socket.emit from Admin text Welcome to the chat app
socket.emit
We will still send an event of newMessage type and the exact same data from text and createdAt. The only difference here is that we'll be generating all the properties as opposed to getting some of them from the user as we did earlier. Let's get started with from. This one will be from Admin. Any time we send a message via the server, we'll call Admin and the text will be our little message, Welcome to the chat app. Next, we'll add createdAt, which will be set equal to new Date by calling the Date().getTime method:
socket.emit('newMessage', {
from: 'Admin',
text: 'Welcome to the chat app',
createdAt: new Date().getTime()
});
Later on, we'll greet them by name. We don't have that information for the moment, so we'll stick with a generic greeting. With this call in place, we can remove the comment and we can move on to the second one. This is the broadcast call that's going to alert every other user, except for the one who joined, that someone new is here.