I recently moved my API:s from Heroku to a Ubuntu droplet provided by digital ocean. I followed these instructions on how to deploy my Node.js/Express API:s and got all of my endpoints working as expected. The only issue is that im able to connect/disconnect to the socket, but not able to emit any messages through the websockets. Everything works fine locally & in heroku with the same code, so I think the issue might be with nginx or ufw firewall.
Client code:
const socket = socketIOClient(https://www.myapp.com/api-with-websockets);
socket.connect();
socket.emit('joinChannel', conversation.id);
Backend:
io.on("connection", (socket) => {
console.log("New client connected");
socket.on("joinChannel", (msg) => {
console.log("New client joined channel:", msg); // <-- never gets called
socket.join(msg);
});
socket.on("disconnect", () => {
console.log("Client disconnected");
});
});
nginx conf:
server {
root /var/www/myapp.com/html;
index index.html index.htm index.nginx-debian.html;
server_name myapp.com www.myapp.com;
location / {
try_files $uri $uri/ =404;
}
location /api-with-websockets/ {
proxy_pass http://localhost:3001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /no-websockets-api/ {
proxy_pass http://localhost:3003/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ~ ^/(socket\.io) {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.myapp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = myapp.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name myapp.com www.myapp.com;
return 404; # managed by Certbot
}
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hi @bobbyiliev ! I managed to fix the issue. Turned out that my client was routing the socket path wrong. I changed the socket options to include resource path to my api and it started working. Thank for your help!
Thanks for the reply @bobbyiliev
The only thing showing up in the Nginx logs is this:
I have been stuck with this issue for couple of days now and found this discussion about opening ports. When I run the command
telnet google.com 3003
im not able to connect, could that maybe cause this problem? I tried opening the port 3003 ufw firewall and by runningiptables -I INPUT 1 -i eth0 -p tcp --dport 3003 -j ACCEPT
but still no luck.Hi there @ollisami,
The configuration actually looks correct.
Do you see any errors in your logs? I could suggest starting with the Nginx error log, first trigger a request, and then use the following command the check the logs:
Let me know how it goes. Regards, Bobby