Node module that provides WebSocket server functionality for express while being compatible with other websocket servers like webpack-hot-client or next.js
const express = require('express');
const wss = require('lup-express-ws')(express());
const app = wss.app;
app.ws("/", function(ws, req){
ws.on('message', function(event){
ws.send("Hello World");
});
});
// or with routers
const router = express.Router();
router.ws("/", function(ws, req){
ws.on('message', function(event){
ws.send("Hello World");
});
});
app.use(router);
You additionally need to install npm install --save-dev @types/ws
import express from 'express';
import expressWs from 'lup-express-ws';
import { WebSocket } from 'ws';
const wss = expressWs(express());
const app = wss.app;
app.ws("/", function(ws: WebSocket, req: express.Request){
ws.on('message', function(event: any){
ws.send("Hello World");
});
});
// or with routers
const router = express.Router() as expressWs.Router;
router.ws("/", function(ws: Websocket, req: express.Request){
ws.on('message', function(event: any){
ws.send("Hello World");
});
});
This module is inspired by the express-ws module! However since the express-ws isn't compatible with other websocket server modules, this one got created.