31 ms·
How is the communication between workers handled? Their blog post about websocket support only advertised the possibility persist state via their key/value stor
by remram 5y ago
How is the communication between workers handled? Their blog post about websocket support only advertised the possibility persist state via their key/value store, but here the workers handling different clients need to communicate with each other in real time. How is this done?
edit: Their "Router" class is a "Durable Object". There is no trace of this in their code, this is configured separately when creating the worker. A durable object is a durable JavaScript object, unrelated to K/V storage; it seems to essentially be a class whose instances act like service workers and can respond to requests using their persistent state. I guess there is special support for WebSocket endpoints in their Durable Object system so that a WebSocket endpoint can be sent to/from the Durable Object.
Their docs are really not optimal, with many different iterations of the syntax mixed together (e.g. some thing only work with the "Module syntax" but every single example is written with the "addEventListener syntax").
- jgrahamc 5y agoThe router code is here: https://github.com/cloudflare/doom-workers/blob/main/router/index.mjs https://github.com/cloudflare/doom-workers/blob/main/router/...
- kentonv 5y agoIntroduction to Durable Objects here: https://blog.cloudflare.com/introducing-workers-durable-objects/ https://blog.cloudflare.com/introducing-workers-durable-obje... It has since gone into open beta. > some thing only work with the "Module syntax" but every single example is written with the "addEventListener syntax" The new modules syntax is still beta -- it'll eventually be the official recommendation for all Workers, but we're not there yet. But it is indeed required for Durable Objects, which is also beta, because we didn't want to come up with an old-style syntax for Durable Objects that would just be deprecated immediately anyway. It does unfortunately leave things in a confusing state at the moment, but it should get better soon.
- unknown_error 5y agoIn short: the vendor (Cloudflare) handles it, not you. Cloudflare has to quickly disseminate a lot of things to their data centers all the time (new files, routing rules, cache invalidations, etc.) and Durable Objects piggybacks on that same infrastructure. From the client's point of view, all the network I/O is to a "centralized" server and not peer-to-peer, except in this case the "centralized" endpoint is actually a cluster of data centers that all talk to each other behind the scenes. Client --> nearest Cloudflare node --> all the other nodes via their phat pipes, not yours Edit: their global stateful serverless chatroom demo more clearly demonstrates how it works: https://github.com/cloudflare/workers-chat-demo https://github.com/cloudflare/workers-chat-demo