35

I've got a proxy running that only hits my node.js server for paths that being with /mysubdir How do I get socket.io configured for this situation?

In my client code I tried:

var socket = io.connect('http://www.example.com/mysubdir');

but then I notice that the underlying socket.io (or engine.io) http requests are hitting

http://www.example.com/socket.io/?EIO=3&transport=polling&t=1410972713498-72`

I want them to hit

http://www.example.com/mysubdir/socket.io.....

Is there something I have to configure on the client and the server?

Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106

4 Answers4

45

In my server I had to

var io = require('socket.io')(httpServer, {path: '/mysubdir/socket.io'})`

In my client I had to

<script src="http://www.example.com/mysubdir/socket.io/socket.io.js"></script>

and also

var socket = io.connect('http://www.example.com', {path: "/mysubdir/socket.io"});`
Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
  • 4
    var io = require('socket.io')(http, {path: '/mysubdir/socket.io'}) - worked for me – reabow Jun 05 '15 at 15:16
  • 1
    Really cool, thx you for your answer. Nota bene, it seems that the '/' in the path is mandatory. I tried 'mysubdir' didn't work out, modified with '/mysubdir' and it worked out. – David Dal Busco Nov 21 '19 at 17:59
11

In my case I am using nginx as a reverse proxy. I was getting 404 errors when polling. This was the solution for me.

The url to the node server is https://example.com/subdir/

In the app.js I instantiated the io server with

var io = require('socket.io')(http, {path: '/subdir/socket.io'});

In the html I used

socket = io.connect('https://example.com/subdir/', {
    path: "/subdir"
});

Cheers,
Luke.

benka
  • 4,732
  • 35
  • 47
  • 58
Luke M
  • 111
  • 1
  • 3
9

Using nginx, this a solution without the need to change anything in the socket.io server app:

In the nginx conf:

location /mysubdir {
   rewrite ^/mysubdir/(.*) /socket.io/$1 break;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_http_version 1.1;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $host;
   proxy_pass http://127.0.1.1:3000;
}

In the server:

var io = require('socket.io')(3000)

In the client:

var socket = io.connect('https://example.com/', {
    path: "/mysubdir"
})
Eugene Primako
  • 2,767
  • 9
  • 26
  • 35
  • I'm using nginx in kubernetes and would like to do this as well. Would you happen to know how to achieve that rewrite pattern in an `Ingress` yaml definition for kubernetes? Issue: https://stackoverflow.com/questions/69194402/nginx-ingress-rewrite-target-with-additional-subpaths – Kajsa Sep 16 '21 at 09:17
  • I'm sorry but no, I did not try using nginx in kubernetes yet. – Emmanuel Geoffray Sep 17 '21 at 10:11
  • 1
    Great answer, but I would suggest small improvement. Leave the rewrite with `rewrite ^/mysubdir/(.*)$ /$1 break;` (this is my uchanged config for subfolder) and simply prefix the path on the client side: `path: (path ? path : '') + '/socket.io'` (I have made this conditional for different settings and this works even localy without subfolder) – Bodzio322 May 08 '22 at 17:08
3

The answer by @Drew-LeSueur is correct for socket.io >= 1.0.

As I was using socket.io 0.9, I found the old way of doing it in the doc.

// in 0.9
var socket = io.connect('localhost:3000', {
  'resource': 'path/to/socket.io';
});

// in 1.0
var socket = io.connect('localhost:3000', {
  'path': '/path/to/socket.io';
});

Notice that a / appears as first character in the new path option.

Community
  • 1
  • 1
yves amsellem
  • 7,106
  • 5
  • 44
  • 68
  • Socket request url is looking fine. but I am getting No 'Access-Control-Allow-Origin' header is present on the requested resource error – Deepak Dholiyan Sep 19 '18 at 10:20
  • @DeepakDholiyan your error seems more related to a CORS issue. Is your javascript hosted on the same origine than your sockets? – yves amsellem Sep 19 '18 at 13:32
  • Now I am getting ERR_CERT_AUTHORITY_INVALID – Deepak Dholiyan Sep 19 '18 at 13:38
  • @DeepakDholiyan assuming you are using socket.IO 0.9, I'm not sure you're having the issue detailed here. Maybe detailing your code and your issue in another stack overflow question will be more helpful. IMO, only telling the error you get make it very difficult for anyone to help you. – yves amsellem Sep 20 '18 at 09:32
  • link of my question:- https://stackoverflow.com/questions/52411188/failed-to-load-resource-neterr-cert-authority-invalid – Deepak Dholiyan Sep 20 '18 at 09:48