104 | Real-Time Revolution: Diving into the World of WebSockets and Long Polling
Published on 2025-09-12
Introduction
Modern users expect web applications to work as fast and responsive as native software. Chats, stock quotes, collaborative document editing — all these scenarios require instant data exchange. In this article, we’ll break down how Long Polling works, why it was replaced by WebSockets, and how to properly configure these technologies on popular web servers.
The Real-Time Problem and the First Solution: Long Polling
HTTP was originally designed for the request-response model: the client contacts the server, the server replies, and the connection closes. For dynamic applications, this is inconvenient.
Long Polling
Long Polling became a compromise solution:
- The client sends a request.
- The server waits for data to appear and does not respond immediately.
- As soon as data is available, the server responds.
- The client receives the response and immediately opens a new connection.
Pros:
- fewer redundant requests compared to regular polling;
- data delivered with minimal delay.
Cons:
- HTTP overhead (headers, sessions);
- scalability issues with thousands of connections.
True Bidirectional Communication: WebSockets
WebSockets radically changed the approach. After an HTTP handshake, the connection is “upgraded,” stays open, and allows the server and client to exchange data both ways.
Key advantages:
- full-duplex — bidirectional communication without extra requests;
- single TCP connection — less overhead;
- low latency — ideal for chats, games, trading platforms;
- efficiency — only the payload is transmitted.
Configuring WebSocket Support
Nginx
In the configuration, it’s important to explicitly pass the Upgrade and Connection headers:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
proxy_http_version 1.1;— required for Upgrade.proxy_set_header Upgrade $http_upgrade;— forwards the protocol.proxy_set_header Connection "upgrade";— enables the upgrade.
HAProxy
HAProxy supports WebSockets out of the box, but it’s important to configure timeouts:
frontend http_front
bind *:80
default_backend http_back
backend http_back
balance roundrobin
server server1 10.0.0.10:8080 check
timeout tunnel 1h
timeout tunnel 1h;— prevents disconnections of long-lived connections.
Caddy
Caddy does everything automatically. A basic configuration is enough:
example.com {
reverse_proxy localhost:8080
}
If more control is needed:
example.com {
reverse_proxy /ws/* localhost:8080 {
header_up -Origin
}
}
Caddy handles Upgrade/Connection itself, removing the need for manual tweaks.
How Does This Make Sites Better?
- Interactivity: chats, notifications, and game actions happen instantly.
- Reduced load: no constant requests → resource savings.
- Efficiency: applications feel responsive and closer to “live.”
Conclusion
Long Polling was the first step toward real time, but today WebSockets are the de facto standard. Understanding how they work and properly configuring proxying on Nginx, HAProxy, or Caddy is an essential skill for modern DevOps engineers and web developers.
Resources
Related reviews
Thanks to Mikhail for his responsiveness. We spoke by phone; he explained how I could do it myself. This is my second time reaching out — everything’s great and prompt.
kireevk · Consultation on Nginx Proxy Manager and Portainer
A customer who has settled in2025-02-25 · ⭐ 5/5
Thanks to Mikhail for his responsiveness. We had a call; he explained how I could do it myself. This is my second time contacting him; everything is great and prompt.
I want to express my deep gratitude to the specialist who set up SEO-friendly URLs for me on OpenCart. Configuring them turned out to be easy and simple, and I'm glad I finally found a professional who did everything well and without unnecessary complications. Before this I went through four specialists, and each time there were issues with the setup, but this person handled the task perfectly.
apande · Configuring Nginx and OpenCart
A very powerful buyer2024-09-07 · ⭐ 5/5
I want to express my great gratitude to the specialist who configured SEO-friendly URLs for me on OpenCart. Setting up the URLs turned out to be easy and simple, and I'm glad that I finally found a professional who did everything efficiently and without unnecessary complications. Before that I changed four specialists, and each time there were problems with the configuration, but this person handled the task perfectly.
Excellent work — this isn't the first time I've used them; they find solutions to complex problems. I recommend.
Evgeni8j · Nginx rate limit
Pro-Expert Buyer2024-05-10 · ⭐ 5/5
Excellent work, this isn't my first time contacting them; they find solutions to complex problems. I recommend.
Excellent work! He completed the assigned task on time and without errors. It was a pleasure to work with him; I recommend him.
Evgeni8j · Nginx configuration
Expert buyer2024-05-03 · ⭐ 5/5
Great job! Completed the assigned task on time and without errors. It was a pleasure to work together, I recommend.
Excellent specialist — he delved into the problem, figured it out, and fixed it. I recommend him.
Evgeni8j · Fixing a regex in an Nginx location block
Expert buyer2024-03-21 · ⭐ 5/5
Excellent specialist, delved into the problem, figured it out, and fixed it. I recommend.
I needed to resolve an SSL certificate issue on a server where the certificate had been issued through Nginx Proxy Manager. Mikhail clarified all the details of my setup and requested access to assess the feasibility of solving the issue, since he hadn't worked with that service before. He quickly figured it out and fixed my problem. Perfect cooperation)
kireevk · Diagnosing Nginx Proxy Manager in a Docker container and resolving the issue
Settled-in customer2024-03-15 · ⭐ 5/5
I needed to solve an issue with an SSL certificate on the server that was issued through Ngnix Proxy manager. Mikhail clarified all the details of how everything is set up for me, asked for access to assess the feasibility of solving the issue, since he had not encountered such a service before. He quickly figured it out and solved my problem. Perfect cooperation)