0

I have a simple node.js app that listens to two ports: on 8001 it sets up a simple webserver by doing

var express = require('express');
var gHttpApp = express();
gHttpApp.use(express.static('public'));
gHttpApp.listen(8080, function () {
    console.log('HTTP Server listening on *:8001');
});

Then, on 8002 it sets up socket.io

var io = require('socket.io')();
gSocket = io.listen(8002);

In my index.html inside the /public folder, I request the socket.io client js by doing:

<script src="http://localhost:8000/socket.io/socket.io.js"></script>

while the other js file are requested with relative path inside /public.

This setup worked while developing locally and seemed logical, but I have no idea how to deploy it on my private server which runs Ubuntu and nginx, since I can not reverse proxy the same location into 2 ports...

David Makogon
  • 2,768
  • 1
  • 20
  • 29
Neenster
  • 101
  • 1

1 Answers1

0

You need a route handler in order to serve "http://localhost:8000/socket.io/socket.io.js".

The socket.io library will serve that file automatically from the port that you have socket.io installed on, but not from any other port. So, in your case, that would be served automatically from port 8082, not from 8080.

So, you could probably change your script tag to:

<script src="http://localhost:8002/socket.io/socket.io.js"></script>

FYI, you can have socket.io and your web server on the same port. No need to use two separate ports. socket.io/webSocket are explicitly designed to work on the same port as your web server. This works because all socket.io/webSocket connections start with an HTTP request anyway. So, socket.io just intercepts that first http request to start a socket.io connection and then all other requests are handled by your web server.

jfriend00
  • 155
  • 6