Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pingtimeout and pingInterval in socket.io

If i set the pingTimeout: 3000 and pingInterval: 2000 ; and if there are simultaneously 800 socket connections; will this have any issue on server performance or provide latency? And what do we mean by when the socket.io docs says that the server will wait for pingTimeout(ms) to disconnect the socket if it doesn't get the pong packet (which means the socket is idle). What cases are covered when we say that the socket is idle? Does that mean that if the user is active on the page but is not clicking anything then does it mean the socket is idle?

like image 392
roh_dev Avatar asked Apr 16 '18 20:04

roh_dev


People also ask

Does Socket.IO use long polling?

First, Socket.IO creates a long-polling connection using xhr-polling. Then, once this is established, it upgrades to the best connection method available.

What is polling in Socket?

The poll() API allows simultaneous connection with all descriptors in the queue on the listening socket. The accept() and recv() APIs are completed when the EWOULDBLOCK is returned. The send() API echoes the data back to the client. The close() API closes any open socket descriptors.

Is Socket.IO synchronous or asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.


1 Answers

socket.io uses an active scheme (as opposed to a passive scheme) for detecting when a socket.io connection has become disconnected or non-functional for any reason.

This active scheme involves the client sending a ping packet to the server on a regular interval and waiting for a pong interval to come back from the server.

From the client-side of things, the pingInterval is how often is sends a ping packet and the pingTimeout is how long is waits for a pong packet to come back from the server before concluding that there is a problem with the connection and it should drop the connection and then make an attempt to reconnect.

Similarly, the server is expecting a ping packet to arrive every pingInterval and if one does not arrive, then the server will conclude that the connection has died in some way.

So, the shorter you set the pingInterval, the more often client is sending a ping and receiving a pong back from the server and similarly, the more often the server is receiving a ping from the client and sending back a pong.

An "idle" socket.io connection is still sending ping and pong messages.

If there are simultaneously 800 socket connections; will this have any issue on server performance or provide latency

There are a lot of variables involved in how many simultaneous connections a server can handle and you don't really provide any of the relevant info to know about your particular case. Here are some of the things that go into determining it:

  1. How fast a network the server is on.
  2. How fast a CPU the server has.
  3. How many other socket.io messages are being sent.
  4. How much other data is being sent over the socket.io connections.
  5. What else the node.js server is being asked to do.
  6. OS configuration (memory, total sockets allowed, etc...).
  7. Server total memory and node.js configured memory.
  8. How often each connection is doing ping and pong.
  9. How often clients are reconnecting (like when they browse around a web-site - every page load drops the previous socket.io connection and has to establish a new one).

In general, 800 simultaneous connections is a relatively small number and many installations can handle that just fine if overall data usage is low and configurations are otherwise optimized. There are some server installations that can handle many thousands of simultaneous connections.

What cases are covered when we say that the socket is idle?

I'm not sure what you mean by "what cases". An idle socket.io connection means that no messages have been flowing in either direction. When the connection goes idle, socket.io uses ping and pong packets to "test" the connection to make sure it is still working.

Does that mean that if the user is active on the page but is not clicking anything then does it mean the socket is idle?

A socket is idle when YOUR code isn't sending or receiving anything on the socket. That may or may not be connected to what the user clicks on. That depends entirely upon your app. For example a stock ticker app may be receiving stock price updates over socket.io every few seconds and that connection would pretty much never be idle even though the end-user never clicked on anything in the web page.

like image 66
jfriend00 Avatar answered Sep 18 '22 18:09

jfriend00