Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-ws

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-ws

WebSocket endpoints for Express applications

  • 5.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
145K
increased by10.86%
Maintainers
1
Weekly downloads
 
Created

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

Keywords

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc