Long Polling – Concepts and Considerations
ably.io
ably.io
For me it all started when Michael Carter worked on orbited, years ago (was it 2008?). Michael made a number of important "discoveries", like the famous document.domain trick:
https://developer.mozilla.org/en-US/docs/Web/Security/Same-o...
Then I played with libevent, trying to create a framework for comet applications. Then redis came along, perfect for backend and Websockets in browsers.
Then of course node.js became a popular engine for messaging applications.
In the mean time we worked on SockJS, to make the transition from comet to websockets smoother. Oh, all those tricks employed in Sockjs...
https://github.com/sockjs/sockjs-client#supported-transports...
There still is a place for things like Opera Eventsource, but it seems websockets over TLS is now the best, stable, and portable way of doing messaging on the web.
Because, it's not a separate technology. It's worked in browsers for years, and is compatible with any HTTP server toolkit/framework that lets you stream a response (so basically all of them).
That said....who would use Comet for long polling? You'd use Comet to avoid the difficulties of long polling.
Long polling: (1) Request (2) Wait (3) Receive an event. (4) Go back to #1.
Streaming Comet: (1) Request (2) Wait (3) Receive an event (4) Go back to #2.
Rather than getting a raw response from the server in a request, streaming Comet loads the response in a iframe. The response is streaming HTML and has a series of <script> tags that post events to the parent window.
It's arguably easier to manage and scale (no missed events, no need to find the same server again), though it certainly looks a little odd.
It would be great if they went into some of the additional architecture they've had to implement to, e.g., prevent denial of service.
[1]: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQS...
That said, I've been working with a middle ground: http 2 + sse. Gives the benefit of simpler infrastructure while not being as inefficient as short/long-polling.
ETA: after using websocket for a while, one of the major downsides is that you can't use most of the http ecosystem. Stuff like cloudflare workers, cloudflare anti-ddos[0], swagger api docs, caching etc. I recently switched to http 2 + sse and do not regret.
[0] - they do support websockets, but at a limited and undisclosed rate (I.e. You don't know how many is too many until they ask you to upgrade your plan).
The various methods to coordinate information between devices on the internet should be looked at objectively and mechanically. At the end of the day, the modern reliable messaging protocols use the IP Frames. The basis of our internet. Layer 6 protocols based on IP Frames are not equal. Many methods are not compatible with the various configurations of networks. The bytes and bandwidth required between each production-ready method differ.
* HTTP/1.1 - 100% Compatibility
* HTTP/2.0 - 100% Compatibility with client initiated connectivity and backward compatibility with HTTP/1
* MQTT - near-full internet wide compatibility ( routing policy / network topology )
* WS - near-full internet wide compatibility ( routing policy / network topology )
Each message received using these mechanisms requires TCP ACKs. The promise of MQTT and WS leads you to believe that the data streaming to your device over WS or MQTT don't require ACKs. However this is not how TCP works. When packets are received there is an associated timeout and retransmission when an ACK is late or missing. Additionally light-weight layer 6 traffic is required to maintain connectivity between two endpoints. Otherwise LRUs and quotas are triggered for routes could be treated as stale, and therefore dropped altogether. This is the underlying mechanism of the layer 6 protocols that are often left out of the discussions.
There is a clear winning approach in my mind. HTTP/2.0 includes, by default, server-initiated data push. The required TLS and header compression, as part of the spec, allow for a secure yet efficient streaming solution. With HTTP/2.0, TCP socket limits are less of a concern, as the client only needs one TCP socket to subscribe to an unlimited number of data feeds. HTTP/1 requires the client to maintain separate sockets for each independent stream, as HTTP/1 enforces head-of-line ordering for muxing. Something we've done special for HTTP/1 clients, we've added multiplexing by allowing multiple topic subscriptions and filter expressions to be passed in a single HTTP call on the same socket. This isn't natively built into HTTP/1 and is supported on all our SDKs.
This is why we have chosen HTTP/2.0 as our next-gen transport protocol. We have started by providing HTTP/2.0 connectivity at our edge for select customers. As of 2018 PubNub is the world-record holder for the largest online concurrent event in human history using HTTP/2.0 for live data streams on a globally celebrated sporting event.
You should be using HTTP/2.0 for your customers. Here is a dockerfile that makes it easy for you to start testing HTTP/2.0 - https://github.com/stephenlb/http2-proxy
Stephen Blum ( @stephenlb ) CTO PubNub