Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does socket.io use polling instead of websockets?

I am pretty new to socket.io and have written my first app in node/express/socket.io. Right now everything works great on my nginx server. I want to release my app to the public, but I am gripped with the fear that it just won't work for a lot of people. I have had a few friends test my app and everything went smoothly (it is a pretty simple app). Here is my concern: Right now every connection seems to be using websockets, which is what i want. But will my app sometimes downgrade to "polling" due to something weird on the clients' end? If so, how does socket.io decide when to use polling and when to use websocket (is it based on browser/version or connection or what)? I am pretty sure it uses websocket when possible, but is there a list somewhere of conditions that will knock it down to "polling"? Also, is there a way I can test my application for by using "polling" to see if it works?

I can post code, but I think this is a general question on how socket.io works.

like image 682
Gilberg Avatar asked Jun 19 '15 00:06

Gilberg


People also ask

Does Socket.IO use polling?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

Why are polls better than WebSockets?

Long polling is more resource intensive on the server than a WebSocket connection. Long polling can come with a latency overhead because it requires several hops between servers and devices.

What will replace WebSockets?

WebTransport is a new specification offering an alternative to WebSockets. For applications that need low-latency, event-driven communication between endpoints, WebSockets has been the go-to choice, but WebTransport may change that.

What is long polling and why would it be beneficial to use?

HTTP Long polling is a mechanism where the server can send data independently or push data to the client without the web client making a request. The information is then pushed as it becomes available, which makes it real-time. However, it works best if the messages from the server are rare and not too frequent.


1 Answers

The only time a client will downgrade to ajax polling (assuming your server does support it which it does) is when the browser client doesn't support webSockets (e.g. a very old client) or perhaps if some proxy in the client path doesn't support webSockets.

webSockets are supported in IE10+ and all recent releases of the other browsers.

So, practically speaking, it's really just IE8 or IE9 or a badly behaved proxy where you might not see client webSocket support.

There are no other conditions (other than lack of support) that will "knock" a connection down to polling.


You can temporarily test your application with polling by only passing in the xhr-polling transport option when connecting from the client to tell the client that that is the only transport option that is allowed.


Keep in mind that all webSocket connections start with an HTTP request that is then "upgraded" to the webSocket protocol if both sides agree so if you're looking at the network trace from your browser, you should see each webSocket connection start with an HTTP request - that is normal. And, in the latest version of socket.io, it may actually exchange a few data packets with the polling transport before it successfully tries and switches to an actual webSocket.

like image 70
jfriend00 Avatar answered Sep 28 '22 09:09

jfriend00