Pull Technology
#1. Short Polling
- Client sends a request to retrieve data from the broker every n seconds
-> Server(Broker) responds back immediately with the data it has.- It can be either none, one, several messages
- Consumes resources
- TCP connection management
- Client authentication/authorization
- Rate Limiting
- TLS termination
- …
#2. Long Polling
- Client sends request to retrieve data from the broker
-> Server(Broker) responds back immediately only if it has data. Otherwise, it does not respond back until it has data - To avoid endless wait on both client and server, the client specifies wait time in the request parameter.
- If wait time reaches, it closes connection
Short Polling vs Long Polling
- Typically Long Polling is preferred
- Long Polling reduces the number of empty responses.
- However, if the client is polling with a single thread, it’s better to use short polling
- Short Polling is better in a usecase if the client expects an immediate response
Push Technology
#1. WebSocket
- Message-based protocol
- Works over HTTP ports 80 and 443
- Re-uses TCP connection initially established for an HTTP request
How does WebSocket work?
- Client sends request with ‘Upgrade‘ header
GET /chat HTTP/1.1Host: server.example.comConnection: UpgradeUpgrade: websocket // THIS!!... - Server responds back
HTTP/1.1 101 Switching ProtocolsConnection: UpgradeUpgrade: websocket...
Advantages of WebSocket
- Fast
- Bi-directional
- Server can send request to the client
- Natively supported by browsers
- Provides an alternative to raw TCP sockets when they are not allowed for security reasons
#2. Server-Sent Events
- Browser(Client) opens up a persistent HTTP connection with the server
- Using keep-alive option
- Server uses the connection to send data to the client
- If the connection drops, client automatically tries to re-connect
- ex) Mail notification system
- When there’s a new email inbox, server sends notification to the browser(client)
Advantages & Disadvantages of Server-Sent Events
Advantages
- Simplicity (Both client-server)
- No need for a custom protocol
Disadvantages
- Mono-directional (server can only push to client)
- Limited to UTF-8 (cannot transmit binary data)
- Browsers limit the number of open connections

Leave a comment