34

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?

4 Answers 4

42

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"});`
2
  • 4
    var io = require('socket.io')(http, {path: '/mysubdir/socket.io'}) - worked for me
    – reabow
    Jun 5, 2015 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. Nov 21, 2019 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.

3
  • 2
    I did, now socket request url is looking fine. but I am getting No 'Access-Control-Allow-Origin' header is present on the requested resource error Sep 19, 2018 at 10:19
  • 1
    I'm getting 'Access-Control-Allow-Origin' error
    – jsirr13
    Aug 12, 2021 at 5:33
  • has anyone found a fix for "'No 'Access-Control-Allow-Origin' header is present on the requested resource error "
    – Huzaifa
    Oct 12, 2021 at 15:08
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"
})
3
  • 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: stackoverflow.com/questions/69194402/…
    – Kajsa
    Sep 16, 2021 at 9:17
  • I'm sorry but no, I did not try using nginx in kubernetes yet. Sep 17, 2021 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 8, 2022 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.

5
  • Socket request url is looking fine. but I am getting No 'Access-Control-Allow-Origin' header is present on the requested resource error Sep 19, 2018 at 10:20
  • @DeepakDholiyan your error seems more related to a CORS issue. Is your javascript hosted on the same origine than your sockets? Sep 19, 2018 at 13:32
  • Now I am getting ERR_CERT_AUTHORITY_INVALID Sep 19, 2018 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. Sep 20, 2018 at 9:32
  • link of my question:- stackoverflow.com/questions/52411188/… Sep 20, 2018 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.