Coder Social home page Coder Social logo

wsmd / ws-multipath Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 2.0 47 KB

๐Ÿ›ฃ Serve multiple WebSocket endpoints using one shared server

License: MIT License

JavaScript 100.00%
websockets websocket-server javascript nodejs node realtime routing multipath http-server

ws-multipath's Introduction

Build Status Coverage Status License

logo

ws-multipath

ws-multipath is a wrapper around websockets/ws that provides the ability to attach multiple WebSocket servers to one shared HTTP server to handle multiple paths.

Why?

Have you ever wanted to serve multiple WebSocket endpoints using a single port/server. You might have tried something that looked a little bit like this:

const { Server } = require('ws');

const PORT = 1234;

const server1 = new Server({ port: PORT, path:'/notifications' });
const server2 = new Server({ port: PORT, path:'/messages' });

Soon after, you realize that it's not possible. Not easily, at least.

Having multiple WebSocket servers on the same port can be a messy process. So I decided to write ws-multipath in hopes of simplifying this task.

How?

In order to have multiple WebSockets server running on the same port, a developer would have to create multiple "serverless" WebSocket.Servers (using the noServer option) and utilize one shared HTTP server that delegates requests to the proper WebSocket server.

Sounds complicated, right? That's all what ws-multipath does!

Example

const MultipathServer = require('ws-multipath');

const server = new MultipathServer({ port: 1234 });

const foo = server.createHandler({ path: '/foo' });
const bar = server.createHandler({ path: '/bar' });

// handle sockets connecting to ws://localhost:1234/foo
foo.on('connection', (ws) => {
  ws.send('hello from /foo!');
});

// handle sockets connecting to ws://localhost:1234/bar
bar.on('connection', (ws) => {
  ws.send('hello from /bar!');
});

Documentation

Please note that this is experimental and under active development. Therefore, it may have numerous bugs, and the API is subject to change.

See /docs/api.md for full API documentation.

Installation

To install ws-multipath, you must have ws installed as well.

# ws is a peer-dependency of ws-multipath

# using npm
npm install --save ws ws-multipath

# using yarn
yarn add ws ws-multipath

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

License

MIT

ws-multipath's People

Contributors

wsmd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

hannesne lareck70

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.