byteball / ocore

Core library for Obyte
https://obyte.org
MIT License
144 stars 93 forks source link

add config to reuse port connection #155

Closed DrSensor closed 5 years ago

DrSensor commented 5 years ago

TL;DR see https://www.npmjs.com/package/ws#multiple-servers-sharing-a-single-https-server

I intend to create this PR because I need the ability to run my bot on Heroku (example, Heroku only expose 1 port and it needs to bind an http server) but seems I was mistaken. I thought in light node mode, it will run WebSocket server but after looking at the source code I realize it only runs WebSocket client.

Anyway, I will leave it here in case you find this handy. Especially if someone want to support/experiment with other socket server/broker 🙂.

How to use Given `conf.js`: ```js exports.reusePort = true ``` If someone want to share port with an http server: ```js const http = require('http'); const server = http.createServer(); const wss = new WebSocket.Server({ server }); require('byteballcore/network').handleUpgradeConnection(server) server.listen(80); ``` Otherwise, for someone who have their own [socket server](https://nodejs.org/docs/latest/api/net.html#net_class_net_server) implementation: ```js const Broker = require('custom-broker'); const {handleUpgradeConnection} = require('byteballcore/network'); const server = new Broker(); server.upgrade( (request, socket, head) => handleUpgradeConnection(request, socket, head) ); server.bind(6331) ```

Other references