I have created a socket.io connection between my react app and my express application.

In my dev environment, this works just fine, code is:

Client

import { io } from "socket.io-client";
const ENDPOINT = process.env.NX_API;
const socket = io(ENDPOINT, { path: '/v1/socket.io' });

Server

const io = require("socket.io")(server, {
    cors: {
        origin: "*",
        methods: ["GET", "POST"]
    }
});

io.on("connection", (socket: any) => {

    socket.on("my_event", (bid: any) => {
        // Parse the message from the websocket
        const parsedMessage: Message = bid
        console.log(parsedMessage)
        const decoded: any = jwt_decode(parsedMessage.authorization);
        // Handle message depending on message_type
        AuctionController.routeMessage(parsedMessage, decoded.sub).then((response) => {
            socket.emit('your-event', response)
        })
    })
    socket.on("disconnect", () => {
        console.log("Client disconnected");
    });
});

Locally this works fine - it starts long polling and quickly switches to ws - the only difference between this code and the code I use in my dev environment is for the DigitalOcean app platform I had to add the path of ’/v1/socket.io’ on my client configuration socket.io as my API is deployed to /v1 and my front end is deployed at /.

In DigitalOcean I see socket.io polling like this :

https://my-app-<hash>.ondigitalocean.app/v1/socket.io/?EIO=4&transport=polling&t=NWGtKyI&sid=4iavO7MifxtfC-w6AAAy

The response is a 200 but it keeps polling.

When I look at the ws side of things it also looks ok:

It has a wss connection that returns 101 (I expected this being the site is over SSL), in the messages section I can see traffic but it looks to just be some numbers being sent up and down the connection and I also notice the websocket connection handshake is initiated over and over again just like the polling (despite the websocket coming back with a 101 response).
.

When I try to run a part of my app that should be sending a message up to the server I get no activity at all, no message going up, no error in the console, no error in the backend.

Unfortunately, I implemented socket.io very recently so this might be just me missing some config I need on the server-side when dealing with wss instead of ws but I thought I might ask here just in case DigitalOcean App platform has some kind of proxy or limitation on such connections which might account for this behavior.

*Please ignore the lax cors setup; I am really just trying to get something working in the app platform before moving on to that.

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.

×
Submit an Answer
2 answers

Having a think about this the issue is that I need to make socket.io aware of the key and the cert for the SSL connection in the options, obviously the app platform handless SSL for me but is there a way I can reference the cert and key?

I wonder if this is related to: https://www.digitalocean.com/community/questions/app-platform-http-2-support

wss can be achieved using a Droplet whose IP is assigned to an A Record of your domain, and then using your own (Let’s Encrypt) cert and key for your domain in your server socket.io code as you mentioned

I’m unsure if we have App Platform offers as much flexibility as Droplets in this aspect

  • I don’t think they are related, gRPC uses http2 as its transfer protocol so as part of that connection it is trying to negotiate a http2 connection.

    I don’t think this applies in my case as the underlying connection for socket.io is http long polling to begin with followed by a wss (ws over tls) connection.

    I think this issue is solely related to me needing the cert and pem location that are used for the tls connection provided by app platform for my api, that way I can specify what certs socket.io should use.

    This is my best guess so far.