The WebSocket Handbook: learn about the technology behind the realtime web

Published on by

The WebSocket Handbook: learn about the technology behind the realtime web image

Standardized a decade ago through RFC 6455, WebSocket has since matured into one of the main technologies powering the modern web. Designed to be event-driven and full-duplex, and optimized for minimum overhead and low latency, WebSockets have become a preferred choice for many organizations and developers seeking to build interactive, realtime digital experiences that provide delightful user experiences.

The WebSocket Handbook is a resource for developers who want to learn about the WebSocket technology and how to use it to build event-driven, realtime apps. The Handbook covers the core building blocks of the WebSocket technology, its characteristics, and advantages. It also provides a step-by-step walkthrough on how to build a realtime web app with WebSockets.

The WebSocket Handbook

What are WebSockets?

Before we talk about the WebSocket Handbook, let's give a brief overview of WebSockets. In a nutshell, WebSocket is a web technology that enables bidirectional, full-duplex communication between client and server over a persistent, single-socket connection. The intent is to provide what is essentially an as-close-to-raw-as-possible TCP communication layer to web application developers.

A WebSocket connection starts as an HTTP request/response handshake. If this initial handshake is successful, the client and server have agreed to use the existing TCP/IP connection that was established for the HTTP request as a WebSocket connection. This connection is kept alive for as long as needed (in theory, it can last forever), allowing the server and the client to independently send data at will in conceptual units referred to as messages. Once the WebSocket connection has served its purpose, it can be terminated via a closing handshake, which both the client and the server can initiate.

High-level overview of a WebSocket connection

WebSockets mark a turning point for web development. Until the emergence of WebSockets, the realtime web was difficult to achieve and slower than we're used to nowadays; it was delivered by using techniques like AJAX and Comet (long) polling that were not truly optimized for realtime applications.

The WebSocket technology has broad applicability. You can use it for different purposes, such as streaming data between backend services, or connecting a backend with a frontend via long-lasting, full-duplex connections. In short, WebSockets are an excellent choice for architecting event-driven systems and building realtime apps and services where it's essential for data to be delivered with immediacy, as soon as it becomes available.

We can broadly group WebSocket use cases into two distinct categories:

  • Realtime updates , where the communication is unidirectional, and the server streams low-latency (and often frequent) updates to the client. Think of live sports updates, alerts, realtime dashboards, or location tracking, to name just a few use cases.
  • Bidirectional communication , where both the client and the server send and receive messages. Examples include chat, virtual events , and virtual classrooms (the last two usually involve features like polls, quizzes, and Q&As). WebSockets can also be used to underpin multi-user synchronized collaboration functionality, such as multiple people editing the same document simultaneously.

Introducing the WebSocket Handbook

The WebSocket Handbook consists of five chapters:

  • Chapter 1: The Road to WebSockets - looks at how web technologies evolved since the inception of the World Wide Web, culminating with the emergence of WebSockets, a vastly superior improvement on HTTP for building realtime web apps.
  • Chapter 2: The WebSocket Protocol - covers key considerations related to the WebSocket protocol. You'll find out how to establish a WebSocket connection and exchange messages, what kind of data can be sent over WebSockets, what types of extensions and subprotocols you can use to augment WebSocket
  • Chapter 3: The WebSocket API - provides details about the constituent components of the WebSocket API — its events, methods, and properties, alongside usage examples for each of them.
  • Chapter 4: Building a Web App** with WebSockets** - provides detailed, step-by-step instructions on how to build an interactive cursor position-sharing demo with WebSockets.
  • Chapter 5: WebSockets at Scale is an overview of the numerous engineering decisions and technical trade-offs involved in building a system at scale. Specifically, a system that is capable of handling thousands or even millions of concurrent end-user devices as they connect, consume, and send messages over WebSockets.

Download your copy:

This version of the WebSocket Handbook does not intend to be exhaustive. In future iterations, we plan to:

  • Add more details to the existing chapters.
  • Provide more examples and walkthroughs for building apps with WebSockets.
  • Cover additional aspects that are currently out of scope, such as WebSocket security, and alternatives to WebSockets.

We hope the Handbook will help you gain a good understanding of how WebSockets came to be and how they work, and make it easier for you to build realtime apps powered by WebSockets.

We always treasure feedback from our readers. If you have questions, any suggestions for what we should include in future versions of the Handbook, or if you simply want to chat about WebSockets, contact us!

If you are interested in quickly and easily building low-latency, scalable, fault-tolerant realtime functionality with WebSockets, sign up for a free account and see what Ably can do for you!

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Visit Paragraph
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article
Basset is an alternative way to load CSS & JS assets image

Basset is an alternative way to load CSS & JS assets

Read article
Integrate Laravel with Stripe Connect Using This Package image

Integrate Laravel with Stripe Connect Using This Package

Read article