Code Monkey home page Code Monkey logo

express-socket-routes's Introduction

expresssocket

An unoppinianated library for the http upgrade event's socket routing

It's used to create socket routes in express

It's usually used in tandom with ws

usage:

const express = require("express");
const app = express();
const _ = require("express-socket-routes")(app);

the "_" is used so that you can ignore the return as the function is of the return type void

Routing:

app.sRoute("/room/:id", (request, socket, head) => {});

using it in your application:

Demo App 1 (demonstrates usage with only one websocket server):

const WebSocket = require("ws");
const webSocketServer = new WebSocket.Server({ noServer: true });

app.sRoute("/room/:id", (request, socket, head) => {
  webSocketServer.handleUpgrade(request, socket, head, (webSocket) => {
    webSocketServer.emit("connection", webSocket, request);
  });
});

Demo App 2 (demonstrates usage with Many websocket servers):

const _ = require("express-socket-routes")(app);
const WebSocket = require("ws");

const webSocketServers = {};

app.sRoute("/room/:id", (req, socket, head) => {
  const roomId = req.params.id;
  let newlyCreatedWebSocketServer;
  if (!webSocketServers[roomId]) {
    // create a new websocket server for this room
    newlyCreatedWebSocketServer = new WebSocket.Server({ noServer: true });

    newlyCreatedWebSocketServer.on("connection", (ws) => {
      // connection handling here
    });
    webSocketServers[roomId] = newlyCreatedWebSocketServer;
  }

  newlyCreatedWebSocketServer.handleUpgrade(req, socket, head, (ws) => {
    newlyCreatedWebSocketServer.emit("connection", ws);
  });
});

Common errors:

  • using "room/:id" instead of "/room/:id" as routes

express-socket-routes's People

Contributors

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