Logo

dev-resources.site

for different kinds of informations.

Understanding WebSocket and creating from Scratch with JavaScript

Published at
5/7/2024
Categories
javascript
websockets
http
tcp
Author
mayanksahai
Categories
4 categories in total
javascript
open
websockets
open
http
open
tcp
open
Author
11 person written this
mayanksahai
open
Understanding WebSocket and creating from Scratch with JavaScript

As developers, we often find ourselves on quests to understand new technologies. This past weekend, while battling a particularly stubborn bug (coffee wasn't helping!), I stumbled upon a cryptic note from my past self: "Explore WebSockets." Curiosity piqued, I set out to learn what these mysterious WebSockets were all about.

WebSockets, in essence, enable real-time, two-way communication between a browser and a server. Unlike regular HTTP requests, which are one-off interactions, WebSockets allow for a continuous exchange of messages, similar to a live chat application.

To understand how this works, I decided to build a simple WebSocket server using JavaScript. This was a learning experience, and the ever-reliable MDN Web Docs became my trusted companion (seriously, those folks are amazing!).

Data Frames: The Messengers of WebSockets

Imagine data traveling between the browser and server in tiny packages called data frames. Each frame carries a specific message and includes control bits that define how the data should be interpreted.

Here's a simplified breakdown of a data frame :

MDN

Decoding the Data Frame:

  • FIN: This bit indicates if the message is complete (FIN = 1) or if it's part of a fragmented message (FIN = 0).

  • Opcode: This identifies the type of message being sent. Common opcodes include text (0x1), binary (0x2), and ping/pong for checking connection health.

  • Mask: This bit (present only in client-to-server messages) specifies whether the payload is masked for security purposes. We'll delve into masking later.

  • Payload Length: This indicates the length of the actual data being carried in the frame. The length can be encoded in one, two, or eight bytes depending on the message size.

  • Masking-key (optional): If the Mask bit is set, this field contains a four-byte key used to unmask the payload for the receiver.

  • Payload: This is the actual data being sent, which could be text, binary data, or any other information.

This is just a simplified view of data frames for single messages. WebSockets can handle more complex scenarios with fragmented messages and masking, but that's a topic for another exploration!

A Toy Project with Big Learning

Building this simple WebSocket server was a great way to gain practical experience with the protocol. It's a toy project, but it opened a door to the fascinating world of real-time communication in web applications.

Check out the Code!

The code for this WebSocket server is available on my GitHub repository: mayank-sahai/socket-js (github.com)

Feel free to explore the code and see how it implements the concepts discussed in this post.

I'll be revisiting the code soon and updating it to delve deeper into functionalities like masking and fragmentation. In the meantime, if you're interested in learning more, check out the excellent resources on MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

Feel free to share your experiences with WebSockets in the comments below!

websockets Article's
30 articles in total
Favicon
Real-Time Voice Interactions with the WebSocket Audio Adapter
Favicon
Boosting WebSocket Scalability through a Python Proxy
Favicon
Q: How can I implement a real-time order book using WebSockets in a cryptocurrency exchange?
Favicon
Unveiling Real-Time Communication: WebSockets and You
Favicon
SockManage — 🔌 Manage single active WebSocket connections per user with Redis-powered persistence
Favicon
Creating a Live Collaborative Editor with Next.js and Sockets.IO
Favicon
webSockets to access websites
Favicon
Scaling Web-Socket to million users
Favicon
Building Real-Time Web Applications with WebSockets and Other Technologies
Favicon
Getting server time via WebSockets and displaying It in Angular application
Favicon
SignalR vs WebSockets: Which One is Better for Real-Time Communication?
Favicon
Building a Real-Time Chat Application with WebSockets
Favicon
Building a Chat App with Websockets and JavaScript Using FastAPI.
Favicon
ProjectEvent
Favicon
ProBook
Favicon
Synchronous and Asynchronous Programming in Python: Key Concepts and Applications
Favicon
WebSockets vs. Real-Time Rivals: A Deep Dive into SSE, Long-Polling, MQTT, and XMPP
Favicon
Enterprise Management System with Real-time Notifications and WebSocket Chat
Favicon
Making a Chess.com clone - 1
Favicon
Building Real-Time Applications with WebSockets and Reactive Streams
Favicon
Build a real-time voting app with WebSockets, React & TypeScript 🔌⚡️
Favicon
Unveiling the Power of WebSockets in Node.js
Favicon
Real-time Magic: Unveiling WebSockets and Firebase
Favicon
Understanding WebSocket and creating from Scratch with JavaScript
Favicon
WebSockets
Favicon
Capture Real-Time Forex Data in Excel with Python and WebSockets!
Favicon
Simple Go Chat Application in under 100 lines of code - Part 1
Favicon
Leveraging Zoom WebSockets with Postman for Real-Time Interactivity - POSTCON 2024
Favicon
Build Real-Time Apps with Eezze
Favicon
Setting up a WebSocket server in Node.js

Featured ones: