Connect via websockets

Is it possible to connect to a websocket server?

Here is a snippet of my server code:

const ws_port = process.env.PORT;
const wss = new WebSocketServer({ port: ws_port });
console.log('listening for connections on %s...', ws_port);

When I deploy this server, I correctly see the log message:

listening for connections on 10000...

Locally, I can connect with
ws://localhost:10000

But when I try connecting to my app using websockets on Render
ws://myservice.onrender.com:10000
my request times out. Nothing shows up in the logs. I assume the ws request is getting blocked.

It seems like a common use case that should be possible. Is there a way to connect via websockets?

Thanks!

Edit: I notice in the docs (Web Services | Render Docs) it says

Only HTTPS requests (port 443) are accepted.

I’m unclear whether that means only secure traffic is allowed, or if it means other protocols are not supported. For instance, can I use secure websockets? (wss://)

Hi there @Aaron_Wilson,
Yes it is possible I have tried it myself !!
here is a picture of the server receiving messages
server

here is a picture of a client sending messages

client

here is the code used for the simple ws server

Sample codes for those who want to try
here is the code for the client

<script>
      var conn = new WebSocket('wss://sok.onrender.com');
      conn.onopen = function(e) {
          console.log("Connection established!");
      };
      setInterval(() => {
        conn.send('Hello server!');
      }, 1000);
      conn.onmessage = function(e) {
          console.log(e.data);
      };
      conn.onclose = function(e) {
          console.log(e.code);
          console.log(e.reason);
      };              
      conn.onerror = function(e) {
          console.log(e);
      };      
  </script>

I have also tried socket.io and it works like a charm just make sure your then your testing in your localhost you do it in https://localhost/app

code for client (socket.io)

<script>
    try{
      console.log("msg")
      var socket = io("socket-lb02.onrender.com", {transports: ['websocket']});
      socket.on('message', function(msg){
        console.log(msg)
        el = document.getElementById('server-time');
        el.innerHTML = 'Message ' + msg;
      });
      setInterval(() => {
        count++
        socket.emit('messaged',"count")
        console.log(count)
      }, 1000);
    }catch (error){
      alert(error)
    };
  </script>

Code for server

Happy coding

Thanks for the detailed response! You helped me get it working.

I was trying to connect using ws://myservice.onrender.com:10000.

Your answer helped me discover two things that I needed to fix:

  1. From the client ws:// does not work - you must use wss:// to connect
  2. From the client, do not specify a port. I needed to drop the :10000 and just use ws://myservice.onrender.com

Now it works like a charm. I’d love to see both of these points added to the official documentation.

3 Likes

As a side note, I’m using the free individual tier to prototype my socket service, and I’m noticing that my connections are closed after 5 minutes - even if they are actively sending messages. I’m hoping this is an artificial (and undocumented?) feature of the free tier.

On the plus side, it’s great for exercising my client’s reconnect logic. :roll_eyes:

Hi there,
It is a known bug we have of the free tier, if you bump up to a paid plan you probably won’t experience this - you can always return to free if it continues,

Regards,

John B

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.