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
BlueOP11mo ago
give me a ping if you can help!! anyone know a solution?
berkant
berkant11mo 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
BlueOP11mo 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
berkant11mo 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
BlueOP11mo 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
berkant11mo 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
BlueOP11mo ago
so i need to upgrade to get this to work?
berkant
berkant11mo 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
BlueOP11mo ago
No description
berkant
berkant11mo ago
You are expected to rewrite parts of your code to accommodate to a serverless runtime like Workers.
Blue
BlueOP11mo ago
this code is written for workers by following the official tutorial
berkant
berkant11mo ago
Source?
Blue
BlueOP11mo 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
berkant11mo 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
BlueOP11mo 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
berkant11mo ago
What I'm saying is, what you want is indeed doable but not without Durable Objects.
Blue
BlueOP11mo ago
okay cool ty thats all i wanted to know thanks
Want results from more Discord servers?
Add your server