How can i use a worker for real time stuff? Limits?

Hi. I can use Workers + websockets for realtime stuff like a chat app. Or Pizza GPS Tracking. E.g Pizza driver client updates in realtime my database with new GPS information, my Worker is subscribed, and delivers updates via websocket to my user. (I have a worker sitting between DB and client for security & auth & ddos reasons)

but what about the timeout? workers bound cannot run longer than 10 seconds, and unbound can’t run longer than 60 seconds. that’s a problem or not?

I tested the Websocket lifetime of bound workers and it easily lasts 10+ hours. If it disconnects, just connect again to the same ID. You can use Durable Objects to keep track of Websocket connections.

2 Likes

thanks a lot! do you send every second a heartbeat to keep the websocket connection alive (so that the worker won’t be terminated)?

1 Like

You should check the readyState if it’s OPEN.

Since this isn’t a server and Worker Requests are closed when they’re done executing, I’ve noticed that it’s rare that a connection is “hanged”, so you shouldn’t need to do ping-pong messaging inside of the connection. It depends on how you intend to use it though, if you proxy the worker websocket to your origin and that connection is lost (but worker still open) - then you’d of course need to do ping-pong. However, if you’re only updating a durable object, which then reflect the message to the client - then it should be reliably open for it’s duration.

1 Like

Now that Durable Objects costs are finalized, I should probably warn about the cost associated.

For Websockets to work, you need to keep a durable object open - doing that for 1M connections for 1 Hour would cost 11 500 $. So be careful

2 Likes

thanks a lot buddy!! there’s already kind of a workaround for that case

but TBH I am still quite disappointed with that pricing :slightly_frowning_face: They said we can use DOs for chat apps and so on. But I can’t imagine DOs for that use case anymore since they are so expensive…

2 Likes

Thanks for sharing the link, but it still isn’t a solution cost-wise for the reasons you mention :cry:

Have to agree with you, at this pricing, it’s not competitive.

2 Likes