JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 205304
  • Score
    100M100P100Q165888F
  • License BSD-2-Clause

WebSocket endpoints for express applications

Package Exports

  • express-ws

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-ws) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

express-ws

WebSocket endpoints for express applications. Gives WebSocket connections access to functionality from express middlewares.

Installation

npm install express-ws

Usage

Add this line to your express application:

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

Now 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.

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

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);
});

server.listen(3000);