Trouble hosting websocket server

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,
});
}
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,
});
}
17 Replies
Blue
Blue4mo ago
give me a ping if you can help!! anyone know a solution?
berkant
berkant4mo ago
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.
Blue
Blue4mo ago
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
berkant
berkant4mo ago
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.
Blue
Blue4mo ago
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
berkant
berkant4mo ago
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.
Blue
Blue4mo ago
so i need to upgrade to get this to work?
berkant
berkant4mo ago
Cloudflare Docs
Cloudflare Durable Objects · Cloudflare Durable Objects docs
Durable Objects provide a powerful API for coordinating multiple clients or users, each with private, transactional and strongly consistent storage …
Blue
Blue4mo ago
No description
berkant
berkant4mo ago
You are expected to rewrite parts of your code to accommodate to a serverless runtime like Workers.
Blue
Blue4mo ago
this code is written for workers by following the official tutorial
berkant
berkant4mo ago
Source?
Blue
Blue4mo ago
Cloudflare Docs
Using the WebSockets API · Cloudflare Workers docs
WebSockets allow you to communicate in real time with your Cloudflare Workers serverless functions. In this guide, you will learn the basics of …
berkant
berkant4mo ago
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.
Blue
Blue4mo ago
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
berkant
berkant4mo ago
What I'm saying is, what you want is indeed doable but not without Durable Objects.
Blue
Blue4mo ago
okay cool ty thats all i wanted to know thanks
Want results from more Discord servers?
Add your server
More Posts