Instant browser refresh with a WebSocket connection
Programming Snapshot – WebSockets

© Photo by Micah Tindell on Unsplash
The WebSocket protocol helps browsers to immediately reload a page if the server-side content changes. In this month's column, Mike Schilli whips up his own WebSocket server in Go for this task.
You may have wondered what kind of magic it takes for, say, a WhatsApp or Slack chat open in your web browser to respond so quickly to new input from your chat partner, or to show that your partner is typing … even before the message has been sent (Figure 1)? To do that, the browser at least needs to partially reload the page you are viewing, but how does it know when to do so?
In the simplest of cases, the browser could just periodically ask the server, but that would generate unnecessary network traffic – after all, nothing would have changed most of the time. This also would cause periodic flickering of what was a largely static page, which would look pretty unprofessional. For a better approach, you could turn the setup on its head and only wake up the browser if the file has changed at the server end.
Unlike with the HTTP protocol, the browser will no longer send a request that the server then responds to before closing the connection. Instead, the browser uses the WebSocket protocol to open a persistent connection to a special server. As soon as it is opened, both the server and the client can send messages to each other, available immediately to the receiver at the other side of the open channel. In other words, the two parties actively listen on their ends and react whenever new information arrives.
[...]
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

News
-
ONLYOFFICE v9 Embraces AI
Like nearly all office suites on the market (except LibreOffice), ONLYOFFICE has decided to go the AI route.
-
Two Local Privilege Escalation Flaws Discovered in Linux
Qualys researchers have discovered two local privilege escalation vulnerabilities that allow hackers to gain root privileges on major Linux distributions.
-
New TUXEDO InfinityBook Pro Powered by AMD Ryzen AI 300
The TUXEDO InfinityBook Pro 14 Gen10 offers serious power that is ready for your business, development, or entertainment needs.
-
Danish Ministry of Digital Affairs Transitions to Linux
Another major organization has decided to kick Microsoft Windows and Office to the curb in favor of Linux.
-
Linux Mint 20 Reaches EOL
With Linux Mint 20 at its end of life, the time has arrived to upgrade to Linux Mint 22.
-
TuxCare Announces Support for AlmaLinux 9.2
Thanks to TuxCare, AlmaLinux 9.2 (and soon version 9.6) now enjoys years of ongoing patching and compliance.
-
Go-Based Botnet Attacking IoT Devices
Using an SSH credential brute-force attack, the Go-based PumaBot is exploiting IoT devices everywhere.
-
Plasma 6.5 Promises Better Memory Optimization
With the stable Plasma 6.4 on the horizon, KDE has a few new tricks up its sleeve for Plasma 6.5.
-
KaOS 2025.05 Officially Qt5 Free
If you're a fan of independent Linux distributions, the team behind KaOS is proud to announce the latest iteration that includes kernel 6.14 and KDE's Plasma 6.3.5.
-
Linux Kernel 6.15 Now Available
The latest Linux kernel is now available with several new features/improvements and the usual bug fixes.