Hello everyone!
In this video I will show you how we can send and receive the data using Socket.io.
But before that, let me show you inside the callback.
Right here we will have a one parameter called a socket.
Inside here we can use socket.id
We will print the unique identifier of the socket connection right here okay.
Every time we refresh the application we will get a different ID okay.
And of course when we open the new connection it will have a connection over here with the new id.
Right now let's see you how we can send the data.
Over.
For example, inside the server.
I want to send the data into the socket.
Okay.
Into particular socket.
Well, in order to send event inside the socket, we will use the emit okay.
With emit we will register the event name for example like welcome okay.
Like that, welcome over here.
And what we need to print right now is I will send the data like hi with the particular id okay.
I will send this message right here for this.
Client.
Okay.
And how we can receive that?
Inside the socket right here.
We will listen in into the event, and we pass the event name.
What's the event name?
It must be exactly the same right here.
Okay.
And we need to pass the callback like message.
And right here I will console log message.
Now let's see.
I will expect and refresh.
Now you can see we already got the hi with this ID.
Now you can see the ID right here.
And the id right here must be exactly the same, right?
Exactly the same.
And if I connect.
right here.
You will see we also got the new client.
Okay, that's how we can register.
And that's what we call the event based API.
Okay.
You can see it's much more cleaner than the previous section.
We we don't need to check every time.
Right.
Instead of we just need to register the event name.
And right here we need to listen in on that.
And of course from the client we can also send the data.
Okay for example like
Message.
My message from client.
And also inside the server we can listen on that
And we need to pass the client.
Message.
Okay.
So now I will refresh and you can see we got the message from the client.
Okay.
Very very cool.
Okay.
We will listen the event on the particular socket.
That's what it means about the socket right here.
It's very similar with the fs.
Sorry.
The "ws" from the previous session.
So that is that's about the video.
I will see you in the next.