node-multi-socketservers
v0.2.11
Published
A multi http & socket servers manager, based on node-multi-webserver
Downloads
9
Maintainers
Readme
node-multi-socketservers
A multi http and socket servers manager, based on node-multi-webserver
Installation
$ npm install node-multi-socketservers
Features
- run the same server with different configurations (ports, ssl, etc...)
- socket communication with all servers
Doc
see node-multi-webservers documentation for webservers management
connection(function eventListener) : return Promise instance
eventListener((socket[ , server ]) => {})emit(string eventName[, mixed data]) : return Promise instance
broadcast(SocketIO.Socket socket, http.Server server, string eventName, [, mixed data]) : return Promise instance
broadcast on all serversremoveAllListeners(string|array eventNames) : return Promise instance
remove all listener of an event or an array of events for all the servers and sockets
Examples
with express
const http = require("http"),
https = require("https"),
express = require("express"),
multiservers = require("node-multi-socketservers");
let servers = new multiservers(),
app = express().get("/", (req, res) => {
res.set("Content-Type", contentType).send(message);
});
return servers.addServer({
port: 80,
name: "http server"
}).then(() => {
return servers.addServer({
port: 443,
name: "secure server",
ssl: true,
key: "YOUR_PRIVATE_KEY",
cert: "YOUR_CERTIFICATE"
});
}).then(() => {
return servers.addServer({
port: 1337,
name: "admin server",
key: "YOUR_PRIVATE_KEY",
cert: "YOUR_CERTIFICATE"
});
}).then(() => {
return servers.setTimeout(2 * 1000);
}).then(() => {
return servers.listen(app);
}).then(() => {
return servers.connection((socket, server) => {
servers.broadcast(socket, server, "newconnection");
socket.on("disconnect", () => {
servers.broadcast(socket, server, "disconnection");
});
});
}).catch((err) => {
console.log(err);
servers.release().catch((err) => {
console.log(err);
});
});
native
const http = require("http"),
https = require("https"),
multiservers = require("node-multi-socketservers");
let servers = new multiservers();
return servers.addServer({
port: 80,
name: "http server"
}).then(() => {
return servers.addServer({
port: 443,
name: "secure server",
ssl: true,
key: "YOUR_PRIVATE_KEY",
cert: "YOUR_CERTIFICATE"
});
}).then(() => {
return servers.addServer({
port: 1337,
name: "admin server",
key: "YOUR_PRIVATE_KEY",
cert: "YOUR_CERTIFICATE"
});
}).then(() => {
return servers.setTimeout(2 * 1000);
}).then(() => {
return servers.listen((req, res) => {
res.writeHead(200, {"Content-Type": contentType});
res.end("hello world, from http:80, https:443 or https:1337");
});
}).then(() => {
return servers.connection((socket, server) => {
servers.broadcast(socket, server, "newconnection");
socket.on("disconnect", () => {
servers.broadcast(socket, server, "disconnection");
});
});
}).catch((err) => {
console.log(err);
servers.release().catch((err) => {
console.log(err);
});
});
Tests
$ npm test