Doubt about the socket.io functioning

Asked

Viewed 358 times

4

I have a question regarding the socket.io that I have already researched in several places and most of a rolled up or uses some obscure terms, but ends up not answering the doubt directly.

The question is whether it works as follows: at the time of the server to send a message to the user it by chance registers the "address" of the user in some way and then sends the direct communication to this address without the user need to make any request (ajax or direct)? And my server can have multiple "logged" users each receiving messages only from another specific user?

1 answer

4


Yes.

The socket.io does nothing more than abstract a websocket connection for you.

The socket connection is between the user and the server, so to perform what you will need to do some type of login, even if it is just a nick (in a chat example) so you can associate the nick with a socket.

You will get a map of nicks/logins that point to your socket.

When you want to create a channel enter user A and B will happen the following scenario:

A sends data to the server. Server receives the data. Server treats data (if necessary) Server forwards data to B.

About receiving messages from a specific user, at the time of dealing via server you can choose to re-upload the data to user B or not, ie, this control is the responsibility of the programmer.

You already have some functional code for testing, maybe I can help you implement what you need/want to do.

  • At the moment I’m not only seeing the working to convert a chat system I have with x checks in x seconds in ajax to these socket schemes. I will search more and I will post other questions thank you.

  • This greatly reduces the bandwidth consumption and server processing when to the format I commented above?

  • Yes, a lot, mainly because you don’t have to open thousands of http connections just to check for new message

Browser other questions tagged

You are not signed in. Login or sign up in order to post.