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