Question

Serverless website but need to use digitalocean for websockets - works on localhost not deployed

Hello!

I have a game hosted on vercel, now i needed socket.io for multiplayer. Everything works on localhost, connected to the server on digitalocean. But it does not work when deployed to vercel even though they are talking to the same ip (on digitalocean) I suspect that localhost and digitalocean can talk to eachother since both are http. Vercel is https, so the requests might not go through? In the browser console i receive "WebSocket connection to ‘wss://123.123.123.123:3001/socket.io/?EIO=4&transport=websocket’ failed: " (No failed explanation, its just empty)

So my question is:

  1. Is my assumption correct? If so
  2. Can i add SSL to a digiocean IP or does it need to be a domain? if so
  3. Can that domain be for example https://server.example.com while https://example.com is used on vercel?

Help greatly appreciated ❤️, preferably some in depth guide as i am not a pro with these kind of things


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Vercel don’t suport websocket client because works as serverless functions returning a bunch of code in each received request, and don’t keep connections alive (necessary for websocket works).

https://vercel.com/guides/do-vercel-serverless-functions-support-websocket-connections

Bobby Iliev
Site Moderator
Site Moderator badge
March 28, 2023

Hi there,

Indeed this sounds like a mixed content problem.

Are you running your web sockets service on a Droplet? If so, as you mentioned, it is best to use a subdomain like server.example.com and follow these steps:

  • Add an A record for your server.example.com subdomain to point to your DigitalOcean server IP address.
  • After that use a free SSL certificate using Let’s Encrypt:

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

  • Then you could add an Nginx reverse proxy to the SSL Nginx server block generated by certbot as described in the tutorial above:
...
location / {
    proxy_pass http://localhost:3001;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
...

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel