abelljs/abell

How do dev-servers of other frameworks/libraries work on single port?

saurabhdaware opened this issue · 2 comments

In abell-dev-server, we have to run a http server on localhost:5000 and a socket server on localhost:3000.

Then we connect the http server to the socket server and every time a change occurs in a file, we send a reload command to http server through websocket.

How is this supposed to work without websocket server?

Oh ok I just found an example in ws library

const http = require('http');
const WebSocket = require('ws');
const url = require('url');

const server = http.createServer();
const wss1 = new WebSocket.Server({ noServer: true });
const wss2 = new WebSocket.Server({ noServer: true });

wss1.on('connection', function connection(ws) {
  // ...
});

wss2.on('connection', function connection(ws) {
  // ...
});

server.on('upgrade', function upgrade(request, socket, head) {
  const pathname = url.parse(request.url).pathname;

  if (pathname === '/foo') {
    wss1.handleUpgrade(request, socket, head, function done(ws) {
      wss1.emit('connection', ws, request);
    });
  } else if (pathname === '/bar') {
    wss2.handleUpgrade(request, socket, head, function done(ws) {
      wss2.emit('connection', ws, request);
    });
  } else {
    socket.destroy();
  }
});

server.listen(8080);

Let's track this in #52