Sling Academy
Home/JavaScript/Manage State Across Sessions with the WebSockets API in JavaScript

Manage State Across Sessions with the WebSockets API in JavaScript

Last updated: December 14, 2024

Managing state across sessions is a crucial requirement for many modern web applications. Persistent state can enhance user experience by allowing users to pick up exactly where they left off, regardless of browser or device restarts. One approach to achieve this is by using WebSockets, an efficient technology that facilitates real-time communication between a client and a server.

Overview of WebSockets

WebSockets provide a bilateral communication channel over a single TCP connection. This full-duplex connection establishes a link where client and server can send messages to each other simultaneously. Unlike traditional HTTP requests, WebSockets hold the potential for changes in the state to be communicated in real-time, enabling better responsiveness in web applications.

Setting Up a WebSocket Server with Node.js

Let’s begin by setting up a basic WebSocket server using Node.js. You’ll need to install the ws library first:

npm install ws

Now, create a simple server in a file called server.js:

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  console.log('A new client connected');
  // Sending a message when a new client connects
  ws.send('Welcome to the WebSocket server!');

  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
    // Broadcasting message to all clients
    wss.clients.forEach(function each(client) {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });
});

Creating a WebSocket Client with JavaScript

Next, let’s create a WebSocket client using JavaScript to connect to our server and manage session state:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>WebSocket Client</title>
</head>
<body>
  <h1>WebSocket Client</h1>
  <script>
    const socket = new WebSocket('ws://localhost:8080');

    socket.onopen = function(event) {
      console.log('Connected to the WebSocket server');
      socket.send('Hello Server!');
    };

    socket.onmessage = function(event) {
      console.log('Message from server ', event.data);
    };

    socket.onclose = function(event) {
      console.log('Disconnected from the WebSocket server');
    };
  </script>
</body>
</html>

Persisting State in WebSocket Sessions

A significant benefit of WebSockets is the ability to keep track of session state on both the server and client. This can be extended further by storing state data in local storage or by passing them through WebSocket messages.

Here’s how we might update our client code to save a chat history across sessions using localStorage:

const socket = new WebSocket('ws://localhost:8080');
let messageHistory = JSON.parse(localStorage.getItem('chatHistory')) || [];

socket.onopen = function(event) {
  console.log('Connected to the WebSocket server');
  // Restore message history when the connection is open
  messageHistory.forEach(msg => {
    const messageElem = document.createElement('div');
    messageElem.textContent = msg;
    document.body.appendChild(messageElem);
  });
};

socket.onmessage = function(event) {
  console.log('Message from server ', event.data);
  messageHistory.push(event.data);
  localStorage.setItem('chatHistory', JSON.stringify(messageHistory));

  const messageElem = document.createElement('div');
  messageElem.textContent = event.data;
  document.body.appendChild(messageElem);
};

Conclusion

Using WebSockets to manage state across sessions in JavaScript can significantly improve the user experience by keeping sessions seamless. Combining WebSockets with implementations such as localStorage allows one not only to maintain a consistent state but also enhances engagement across different client sessions. By implementing these techniques, developers can offer real-time, persistent, and responsive applications effectively.

Next Article: Push Data Instantly to Clients Using JavaScript WebSockets

Previous Article: Build Chat Apps and Multiplayer Games via JavaScript WebSockets

Series: Web APIs – JavaScript Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration