Running socket.io on Shared Hosting

2020-07-30 04:19发布

问题:

I am using a shared hosting server from A2Hosting where I want to run a socket.io server (application), here is what I have done so far:

  1. SSH'ed into server
  2. Installed node
  3. Run / started the socket.io server (application)
var server = require('http').createServer(),
    io = require('socket.io')(server),
    port = 58082;

server.listen(port, my - domain - name);

But my client (the browser) can't connect to the server.

I have tried running the same socket.io sever (application) on a local Linux machine and I was able to successfully connect via the browser, so the issue lies in the configuration of the shared hosting server.

回答1:

You're almost there. The one thing missing is the integration of your socket.io application with the webserver. For that you will need a .htaccess file to redirect the incoming requests.

Create a .htaccess file in the public_html directory and add the snippet below. Replace the XXXXX with an unused port anywhere between 49152 and 65535, those are the ones available. If your application won't start, try a different port.

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]

If you need a more detailed guide / sources:

  • How to install and configure Node.js on managed hosting accounts