uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.08k stars 573 forks source link

Differences when running on local via running on a server #519

Closed Adidi closed 3 years ago

Adidi commented 3 years ago

There is some inconsistencies when I run my app on my local machine (locahost) then when i run it on actual server(with some domain) for example the new version 19.2 which supposed to send ping to the client automatically: in my local machine i get the pong messages from the client automatically (i console.log in the server the pong messge) but when i run the same version with the same code in my staging environment i dont get this ping-pong automatically and the client always disconnect after 60 seconds! Is this make any sense ? do i need to run the server in specific way when i run on production (lets say with ssl) ? why the same code on my local machine run differently then when i run it with specific domain in my remote real server ?

ghost commented 3 years ago

You are using NPM, aren't you?

Adidi commented 3 years ago

Mmmm I am using yarn - i have to use npm ? this is the line from package.json: "uWebSockets.js": "uNetworking/uWebSockets.js#v19.2.0"

hst-m commented 3 years ago

Open the network tab in your browser console to look at the headers sent from server to confirm server is using v19, need to check that first

Adidi commented 3 years ago

This is not browser -it's mobile application and I am sure its running v19 - I see even in the server itself the files. Could it be something with nginx reverse proxy configuration in my server ? In my local machine I am connecting to node itself via port but in my server I have nginx running this node as a reverse proxy. this is my configuration:

http {
   map $http_upgrade $connection_upgrade {
      default upgrade;
      '' close;
   }
   upstream websocket {
      server 127.0.0.1:9006;
   }
   server {
      listen 80;
      server_name chat.mydomain.com;

      location / {
         proxy_pass http://websocket;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $connection_upgrade;
      }
   }
}

The connection works well - i can connect to this server web socket but i disconnect every minute regardless and if the phone goes idle it also always disconnected. Could it be nginx configuration problem ?

hst-m commented 3 years ago

Is your idleTimeout set to 60 seconds? You said it disconnects after 60 seconds, the default is 120

Adidi commented 3 years ago

@hst-m I didn't set it at all. Since v19 sends pings automatically so I want to work with the default why should i change it ?

hst-m commented 3 years ago

Ok you missed the point, something else is disconnecting the connection because uWS closes connection after 120 seconds

Adidi commented 3 years ago

Ok I think i find the problem - it is nginx configuration - you need to add proxy_read_timeout this is my configuration now:

location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_read_timeout 7d;
        }

and i get the pong again.

Thanks !

hst-m commented 3 years ago

Yep that was it, proxy_read_timeout defaults to 60 seconds