Skip to content
Justice Tech School

WebSockets and Server Sent Events

WebSockets keep a two way connection open for chat and live updates. Server sent events are one way streams. The ws package is the common choice. Not run here because it needs a client.

Example
const { WebSocketServer } = require('ws');
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', (socket) => {
  socket.on('message', (data) => socket.send('echo: ' + data));
});

Exercise

Send a message.

socket.("hi")

Quiz Test

What do WebSockets provide?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.