Instant browser refresh with a WebSocket connection

Programming Snapshot – WebSockets

© Photo by Micah Tindell on Unsplash

© Photo by Micah Tindell on Unsplash

Article from Issue 296/2025
Author(s):

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.

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Security Lessons: HTML5

    New web technologies address shortcomings in web browsers but create new problems as well.

  • WebRTC Protocol

    The WebRTC protocol converts your web browser into a communications center, supporting video chat over a peer-to-peer connection without the need for helper apps or browser plugins.

  • Why HTTP/2?

    HTTP/1.1 was advanced for its time, but it wasn't designed for today's complex websites. We'll show you why the new HTTP/2 specification provides a solution.

  • Clojure

    Clojure looks like Lisp and runs wherever Java is installed. Practical tools and sophisticated libraries provide the underpinnings for rapid development of modern web applications.

  • SPICE

    Red Hat peps up performance of multimedia applications on remote desktops with SPICE (Simple Protocol For Independent Computing Environments).

comments powered by Disqus
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.

Learn More

News