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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With