https://discord.cloudflare.com logo
Join Discord
Powered by
# durable-objects
  • d

    Dani Foldi

    05/21/2023, 4:21 PM
    why not use a single DO, so you can keep track of all (other) websockets?
  • q

    Quân Hoàng

    05/21/2023, 4:22 PM
    im new to cloudfare workers and DO so could you elaborate more on this
  • d

    Dani Foldi

    05/21/2023, 4:22 PM
    I believe the dash today doesn't support declaring migrations, so you'll need to use wrangler to upload your DO - you can also bind it to other workers afterwards via the dash though
  • d

    Dani Foldi

    05/21/2023, 4:25 PM
    When a new client connects, you give them a websocket from the DO (see docs here https://developers.cloudflare.com/workers/runtime-apis/websockets/), and you can create an instance variable on the DO class say
    sockets
    , to which you add all the connected sockets. When you get a message (see
    message
    event above), you can broadcast to all other websockets, you can check for equality with
    ===
    in this case to filter the sender out from the array
  • q

    Quân Hoàng

    05/21/2023, 4:27 PM
    but is it sort of single point of failure? i mean storing all socket connections within just single DO, is it a problem when scaling multiple users?
  • q

    Quân Hoàng

    05/21/2023, 4:29 PM
    how about having an external message queue (like rabbitmq or sth like that) and implement pub-sub mechanism to fanout online status of a client to all of its friends (in a chat system for example)? what do you think about that approach
  • c

    carsonem

    05/21/2023, 9:23 PM
    Hey, has anyone run into this issue with durable objects using miniflare: TypeError: mf.getDurableObjectNamespace is not a function.
  • c

    carsonem

    05/21/2023, 9:24 PM
    //my script
    Copy code
    import { Miniflare } from 'miniflare';
    
    const mf = new Miniflare({
        modules: true,
        durableObjects: { TEST_OBJECT: 'TestObject' },
        script: `
       export class TestObject {
         constructor(state) {
           this.storage = state.storage;
         }
     
         async fetch(request) {
           const url = new URL(request.url);
           if(url.pathname === "/put") await this.storage.put("key", 1);
           return new Response((await this.storage.get("key")).toString());
         }
       }
    
       export default {
          async fetch(request, env) {
            const stub = env.TEST_OBJECT.get(env.TEST_OBJECT.idFromName("test"));
            return stub.fetch(request);
          }
        }
       `
    });
    
    let res = await mf.dispatchFetch('http://localhost:8787/put');
    console.log(await res.text()); // "1"
  • c

    carsonem

    05/21/2023, 9:27 PM
    I am new to durable objects and I just copied the example to get started.
  • c

    carsonem

    05/21/2023, 11:12 PM
    Update: downgraded to 2.14 and it worked Update: upgraded back to 3.0.0 and got it working again
  • a

    AA

    05/21/2023, 11:57 PM
    hmm, i wonder if you put a PG pool or something into durableobjects.. i wonder how long it stays active if nothings currently connecting to it
  • a

    AA

    05/21/2023, 11:58 PM
    dont even know how to measure that 😛
  • l

    Larry

    05/22/2023, 2:27 AM
    Cloudflare pub-sub (which is MQTT rather than RabbitMQ) is in closed beta but you can get in if you go over to that board and ask
  • a

    arbfay

    05/22/2023, 10:54 AM
    Anyone else experiencing issues with adding a new DO to an existing worker? Been 2 days, still nothing in #895794943182909470
  • d

    Dani Foldi

    05/22/2023, 10:56 AM
    Worked fine for me during the weekend, what sort of issue are you seeing?
  • a

    arbfay

    05/22/2023, 10:57 AM
    Just saying the script is not exporting the class, whilst it clearly is (exactly like the 3 others created before)
  • d

    Dani Foldi

    05/22/2023, 11:01 AM
    Did you also add a [[migrations]] entry in wrangler.toml? You have to add it whenever you create a new DO class
  • a

    arbfay

    05/22/2023, 11:02 AM
    Yeah but I just tried via a new tag and it worked. I'm not sure that's how it worked before, I used to to just add class names in the same migration tag
  • a

    arbfay

    05/22/2023, 11:05 AM
    I used to just add the new class to the latest "new_classes" migration, as my git history shows. This behaviour changed
  • c

    ckoeninger

    05/22/2023, 7:50 PM
    yeah still not 100% ready, wait for that docs PR to be merged before trying out hibernation api
  • i

    Iann

    05/23/2023, 9:52 PM
    is there an API that gives the size of a durable object ?
  • i

    Iann

    05/23/2023, 11:20 PM
    I tried searching every corner of the docs and it doesn't seem to have such thing, this is kind of surprising that this API is not available
  • l

    Larry

    05/24/2023, 9:40 PM
    I don't really care how much storage it's taking up, but I would love a way to find out how much memory it and all of its siblings running in the same 128MB are taking up.
  • c

    crabmusket

    05/25/2023, 1:07 AM
    or at least sending that information out as part of the log streaming, so we could track statistics across a fleet of objects 🤔
  • i

    Iann

    05/25/2023, 1:48 AM
    Yeah, either an API or something like that, it's specially annoying atm to monitor and meter SaaS products built on top of durable objects.
  • i

    Iann

    05/25/2023, 1:48 AM
    Or providing a free version of the product that has a storage limit
  • s

    sdan

    05/25/2023, 2:29 AM
    https://edge-chat-demo.cloudflareworkers.com/ curious how this works? for example if i go to a room named "test" how can i see messages there still? how can the durable object stay open unless someone or some service is keeping that websocket open? am i getting this right?
  • k

    kian

    05/25/2023, 2:30 AM
    Durable Objects have persistent storage available through an API
  • k

    kian

    05/25/2023, 2:30 AM
    https://github.com/cloudflare/workers-chat-demo/blob/master/src/chat.mjs#L293C31-L295
  • s

    sdan

    05/25/2023, 2:31 AM
    understood. so how long is this "persistent" storage available for? where is it stored?

    https://cdn.discordapp.com/attachments/773219443911819284/1111119195405566033/Screenshot_2023-05-24_at_7.30.44_PM.png▾

1...561562563...567Latest