Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets listening on UNIX domain socket?

Is it possible to setup a WebSockets server behind an nginx server which handles connections on a UNIX domain socket?

I currently have several WebSocket server instances on the same machine and have the problem of port sharing. All instances must be allocated a unique port. I want to avoid that and instead communicate between nginx and the WebSockets backend using UNIX domain sockets.

The WebSockets library I'm using is ws (https://github.com/einaros/ws)

I currently create my server like this:

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({host: "127.0.0.1", port: 8080});
like image 430
Markus Johansson Avatar asked Feb 13 '23 21:02

Markus Johansson


1 Answers

Server side:

var http = require('http');
var WebSocketServer = require('ws').Server;
var server = http.createServer()
var wss = new WebSocketServer({ server : server});
server.listen('/tmp/server.sock')

Client side:

var ws = require('ws');
var client = new ws("ws+unix:///tmp/server.sock")
client.send('hello')
like image 107
Nahuel Greco Avatar answered Feb 15 '23 10:02

Nahuel Greco