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
-
Linux Hits an Important Milestone
If you pay attention to the news in the Linux-sphere, you've probably heard that the open source operating system recently crashed through a ceiling no one thought possible.
-
Plasma Bigscreen Returns
A developer discovered that the Plasma Bigscreen feature had been sitting untouched, so he decided to do something about it.
-
CachyOS Now Lets Users Choose Their Shell
Imagine getting the opportunity to select which shell you want during the installation of your favorite Linux distribution. That's now a thing.
-
Wayland 1.24 Released with Fixes and New Features
Wayland continues to move forward, while X11 slowly vanishes into the shadows, and the latest release includes plenty of improvements.
-
Bugs Found in sudo
Two critical flaws allow users to gain access to root privileges.
-
Fedora Continues 32-Bit Support
In a move that should come as a relief to some portions of the Linux community, Fedora will continue supporting 32-bit architecture.
-
Linux Kernel 6.17 Drops bcachefs
After a clash over some late fixes and disagreements between bcachefs's lead developer and Linus Torvalds, bachefs is out.
-
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.