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

December 26, 2018 1.6k views
Development Ubuntu 18.04

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

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04

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

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04

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

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 s1.myserver.com:port 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:

https://www.tutorialspoint.com/html5/html5_websocket.htm

Alternatively if an different server type/configuration would be required to achieve this I'm happy to move the websites/projects to s2.myserver.com 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 s3.myserver.com be used for websockets alone).

2 Answers

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 :)

Jarland

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.

Have another answer? Share your knowledge.