#Trouble hosting websocket server

27 messages · Page 1 of 1 (latest)

wind sierra
#

Im trying to create a websocket that just shares all the data send through it. If client A sends the data {"type":"minecraft"} to the server, all other clients recieve that data execpt for the one that send it. This is what i have so far but nothing is being recieved anywhere at all, ive tried testing using Python, Vanilla JS, Postman, NodeJS and nothing is working. any help would be amazing!

let connections = [];

addEventListener("fetch", (event) => {
    event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
    const upgradeHeader = request.headers.get("Upgrade");
    if (!upgradeHeader || upgradeHeader !== "websocket") {
        return new Response("Expected Upgrade: websocket", { status: 426 });
    }

    const webSocketPair = new WebSocketPair();
    const client = webSocketPair[0],
        server = webSocketPair[1];

    connections.push(server);

    server.addEventListener("message", (message) => {
        connections.forEach((connection) => {
            connection.send(message.data);
        });
    });

    server.addEventListener("close", () => {
        connections = connections.filter((conn) => conn !== server);
    });

    return new Response(null, {
        status: 101,
        webSocket: client,
    });
}

#

give me a ping if you can help!!

wind sierra
#

anyone know a solution?

little scaffold
#

Well, you should probably look into Durable Objects at this point because having a global list of connections as it is right now wouldn't really help as far as Workers runtime is concerned.

wind sierra
#

im only planning on having 2 or 3 clients connected at once and i dont really have the money to upgrade to a paid plan

little scaffold
#

You should treat your Workers code more like shared-nothing just as how PHP does it.

#

Having a top level variable is syntactically correct and allowed.

wind sierra
#

i dont know any php or how it works :/

#

i just want to know if this code is wrong or if i have a setting wrong in my worker

little scaffold
#

But the runtime itself could have thousands of isolates with each having a different internal state and execution context, which means making changes to connections variable isn't going to propagate to other connections.

#

Durable Objects it is.

wind sierra
#

so i need to upgrade to get this to work?

little scaffold
wind sierra
little scaffold
#

You are expected to rewrite parts of your code to accommodate to a serverless runtime like Workers.

wind sierra
#

this code is written for workers by following the official tutorial

little scaffold
#

Source?

wind sierra
little scaffold
#

I think you should get an understanding of how Workers work.

#

That only guides you on how to set up a server and exchange messages with the connecting client itself.

#

Not other clients.

wind sierra
#

okay yes i know that

#

thats why im here

#

im asking how i could adapt it to work the way i want

#

my edits arent working

little scaffold
#

What I'm saying is, what you want is indeed doable but not without Durable Objects.

wind sierra
#

okay cool ty