🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

express-ws

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
e

express-ws

WebSocket endpoints for Express applications

5.0.2
latest
100

Supply Chain Security

100

Vulnerability

100

Quality

77

Maintenance

100

License

Version published
Weekly downloads
196K
8.99%
Maintainers
1
Weekly downloads
 
Created
Issues
82

What is express-ws?

The express-ws package is an extension for the Express.js framework that adds WebSocket support. It allows you to easily set up WebSocket endpoints alongside your existing HTTP routes, enabling real-time communication between the server and clients.

What are express-ws's main functionalities?

Setting up a WebSocket endpoint

This code sets up a basic WebSocket endpoint at '/echo' that echoes back any message it receives. The express-ws package is initialized with the Express app, and a WebSocket route is defined using app.ws().

const express = require('express');
const expressWs = require('express-ws');
const app = express();
expressWs(app);

app.ws('/echo', (ws, req) => {
  ws.on('message', (msg) => {
    ws.send(msg);
  });
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Broadcasting messages to all connected clients

This code sets up a WebSocket endpoint at '/broadcast' that broadcasts any received message to all connected clients except the sender. It maintains a list of connected clients and iterates through them to send the message.

const express = require('express');
const expressWs = require('express-ws');
const app = express();
expressWs(app);

let clients = [];

app.ws('/broadcast', (ws, req) => {
  clients.push(ws);
  ws.on('message', (msg) => {
    clients.forEach(client => {
      if (client !== ws && client.readyState === 1) {
        client.send(msg);
      }
    });
  });
  ws.on('close', () => {
    clients = clients.filter(client => client !== ws);
  });
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Integrating WebSocket with existing HTTP routes

This code demonstrates how to integrate WebSocket endpoints with existing HTTP routes. It sets up a simple HTTP GET route at '/' and a WebSocket route at '/ws'.

const express = require('express');
const expressWs = require('express-ws');
const app = express();
expressWs(app);

app.get('/', (req, res) => {
  res.send('Hello, HTTP!');
});

app.ws('/ws', (ws, req) => {
  ws.on('message', (msg) => {
    ws.send('Hello, WebSocket!');
  });
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Other packages similar to express-ws

FAQs

Package last updated on 07 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts