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
-
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.
-
Microsoft Makes Surprising WSL Announcement
In a move that might surprise some users, Microsoft has made Windows Subsystem for Linux open source.
-
Red Hat Releases RHEL 10 Early
Red Hat quietly rolled out the official release of RHEL 10.0 a bit early.
-
openSUSE Joins End of 10
openSUSE has decided to not only join the End of 10 movement but it also will no longer support the Deepin Desktop Environment.
-
New Version of Flatpak Released
Flatpak 1.16.1 is now available as the latest, stable version with various improvements.
-
IBM Announces Powerhouse Linux Server
IBM has unleashed a seriously powerful Linux server with the LinuxONE Emperor 5.
-
Plasma Ends LTS Releases
The KDE Plasma development team is doing away with the LTS releases for a good reason.