How do I setup my Ubuntu 18.04 server to allow for WebSockets?

I have an Ubuntu-18.04 server (which we will say has the hostname for the sake of ease) that was set up on DigitalOcean using these 2 tutorials (so you know what it have installed etc currently):

And then I’ve used these 2 tutorials for each new website/project on the server (for the vhosts and ssl):

I have a number of website based projects which use frequent AJAX requests which use a lot of the droplet/servers CPU which needs optimizing, so I looked for alternatives. In looking for ways to optimize this I discovered WebSockets. When I tested various examples that used the I found online locally using XAMPP I managed to get them to work, however when I tried the same code on my server I kept getting a variety of errors which led to me realize that my server probably doesn’t support WebSockets.

I’m new to server-side stuff currently so whilst I know that a lot of the tutorials I’ve read for things such as Nodejs and similar systems are the way to go, for what I’m trying to achieve I keep finding tutorials that are either way to basic to achieve my goal or assume way to much prior knowledge to help.

What I ideally want is to replace my current system of “multiple users using numerous Ajax requests each sending and receiving to and from the database for updates every couple of seconds” with the JavaScript WebSocket to connect to and be able to send/receive JSON information with the server to get updates. Similar to that which is seen in tutorials such as this:

Alternatively if an different server type/configuration would be required to achieve this I’m happy to move the websites/projects to with said different setup.

Alternatively again I’m happy to simply have a second droplet for websockets alone (I have a lot of projects that I’d upgrade to using them if I can get it to work - so this wouldn’t be a bad system if that’s doable - I’d happily have the hypothetical be used for websockets alone).

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Did you find a solution?

I am trying to implement web sockets and struggling a lot. I am not able to get success establishing socket connection.

I am using droplet with Ubuntu 18.04, I installed LAMP and NodeJs, I am using SailsJs framework. When I try to load the Sails home page, it give this error:

Error during WebSocket handshake: Unexpected response code: 400

I am struct at this, also raised a ticket but did not get any reply.

Please help.

Hey friend,

There shouldn’t need to be any extra configuration to the server to support websockets. As long as your application is designed to handle them, and the user’s browser supports it then it should work pretty well out of the box with whatever web server configuration you need to serve your web application itself.

If you run into trouble along the way, feel free to ask for help along the way. Just give us a rundown of the events and the errors, and myself or anyone else reading here will at least have the opportunity to see if it’s something we can weigh in on :)