GithubHelp home page GithubHelp logo

the-cc-dev / express-ws Goto Github PK

View Code? Open in Web Editor NEW

This project forked from henningm/express-ws

0.0 1.0 0.0 50 KB

WebSocket endpoints for express applications

License: BSD 2-Clause "Simplified" License

JavaScript 100.00%

express-ws's Introduction

express-ws Dependency Status

WebSocket endpoints for Express applications. Lets you define WebSocket endpoints like any other type of route, and applies regular Express middleware. The WebSocket support is implemented with the help of the ws library.

Installation

npm install --save express-ws

Usage

Full documentation can be found in the API section below. This section only shows a brief example.

Add this line to your Express application:

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

Important: Make sure to set up the express-ws module like above before loading or defining your routers! Otherwise, express-ws won't get a chance to set up support for Express routers, and you might run into an error along the lines of router.ws is not a function.

After setting up express-ws, you will be able to add WebSocket routes (almost) the same way you add other routes. The following snippet sets up a simple echo server at /echo. The ws parameter is an instance of the WebSocket class described here.

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

It works with routers, too, this time at /ws-stuff/echo:

var router = express.Router();

router.ws('/echo', function(ws, req) {
  ws.on('message', function(msg) {
    ws.send(msg);
  });
});

app.use("/ws-stuff", router);

Full example

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

app.use(function (req, res, next) {
  console.log('middleware');
  req.testing = 'testing';
  return next();
});

app.get('/', function(req, res, next){
  console.log('get route', req.testing);
  res.end();
});

app.ws('/', function(ws, req) {
  ws.on('message', function(msg) {
    console.log(msg);
  });
  console.log('socket', req.testing);
});

app.listen(3000);

API

expressWs(app, server, options)

Sets up express-ws on the specified app. This will modify the global Router prototype for Express as well - see the leaveRouterUntouched option for more information on disabling this.

  • app: The Express application to set up express-ws on.
  • server: Optional. When using a custom http.Server, you should pass it in here, so that express-ws can use it to set up the WebSocket upgrade handlers. If you don't specify a server, you will only be able to use it with the server that is created automatically when you call app.listen.
  • options: Optional. An object containing further options.
    • leaveRouterUntouched: Set this to true to keep express-ws from modifying the Router prototype. You will have to manually applyTo every Router that you wish to make .ws available on, when this is enabled.
    • wsOptions: Options object passed to WebSocketServer constructor. Necessary for any ws specific features.

This function will return a new express-ws API object, which will be referred to as wsInstance in the rest of the documentation.

wsInstance.app

This property contains the app that express-ws was set up on.

wsInstance.getWss()

Returns the underlying WebSocket server/handler. You can use wsInstance.getWss().clients to obtain a list of all the connected WebSocket clients for this server.

Note that this list will include all clients, not just those for a specific route - this means that it's often not a good idea to use this for broadcasts, for example.

wsInstance.applyTo(router)

Sets up express-ws on the given router (or other Router-like object). You will only need this in two scenarios:

  1. You have enabled options.leaveRouterUntouched, or
  2. You are using a custom router that is not based on the express.Router prototype.

In most cases, you won't need this at all.

Development

This module is written in ES6 and uses ESM.

express-ws's People

Contributors

henningm avatar omgimalexis avatar joepie91 avatar lavawong avatar alvarocastro avatar theasp avatar piranna avatar mdvanes avatar maxtruxa avatar wsmind avatar emersion avatar nicholi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.