Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

websockets

What are websockets  •  How do they work  •  How to use them  • 


What are websockets

Allow bi-directional connection between cilent and server that stays open until either closes it. I.e. server can send stuff to client without being requested from client.

  • bi-directional: client/ server communicate between each other
  • full-duplex: can send and receive data simultaneously
  • persistent: connection stays open until server or client closes it

How do they work

Client requests to upgrade to websockets when sending HTTP request. Looks like this:

GET /socket HTTP/1.1
Host: localhost:3000
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13

How to use them

Bun

Bun abstracts this upgrade with a nice upgrade method:

Bun.serve({
  fetch(req, server) {
    // upgrade the request to a WebSocket
    if (server.upgrade(req)) {
      return; // do not return a Response
    }
    return new Response("Upgrade failed :(", { status: 500 });
  },
  websocket: {}, // handlers
});

Node

ws lib

import * as WebSocket from 'ws';

const server = new WebSocket.Server({ port: PORT });

server.on('connection', ws => {
    ws.on('message', message => {
        console.log(`Received message => ${message}`);
    });
    ws.send('Hello! You are connected.');
});

Zig

Way more fucking work lmao

About

Notes about web sockets

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors