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

    HardAtWork

    08/22/2021, 7:38 PM
    That's a good idea. How would it handle getting prematurely shut down, though? Would there be a way to store a reference to a connection in Storage?
  • m

    Mallissin

    08/22/2021, 7:41 PM
    The DO being shut down or the incoming stream of audio?
  • m

    Mallissin

    08/22/2021, 7:42 PM
    I assume the chunks would be state.storage. The chunked stream itself would just stop when the manifest stopped being updated.
  • m

    Mallissin

    08/22/2021, 7:43 PM
    (and there would have to be a cleanup mechanism to remove stale chunks and such)
  • e

    Erwin

    08/23/2021, 5:09 AM
    My first instinct would be for the rooms to be their own DO and the Lobby to be another. And the room DOs can update the Lobby DO as needed. Then depending on how many people would be in a lobby I would connect them straight to the DO, or go via something like KV
  • r

    raRaRa

    08/23/2021, 9:38 AM
    Hmm yeah, that's very interesting. I keep hearing about 'performance issues', is it because all messages sent to a DO are processed synchronously?
  • r

    raRaRa

    08/23/2021, 9:39 AM
    So if we have 10-20 rooms, where people are joining/leaving, it can't be that bad.
  • e

    Erwin

    08/23/2021, 10:31 AM
    It is scaling (per DO) issues. They are very horizontally scalable. At 10/20 rooms you’ll be fine!
  • r

    raRaRa

    08/23/2021, 10:42 AM
    Ok super. Can't wait to experiment more with this!
  • m

    mz

    08/23/2021, 11:15 AM
    Hi, we are stuck at the same problem (Workers as a websocket client to DurableObject). How did you solve it?
  • i

    ItsWendell

    08/23/2021, 11:19 AM
    I think in the end it was a stupid mistake, I did a POST request with a body holding some information hoping that I'd get a websocket back but this shouldn't work and doesn't work according to the websocket standards. It should always be a GET request.
  • m

    mz

    08/23/2021, 11:21 AM
    So, switching to GET from a Worker sending an upgrade request to the DO worked?
  • i

    ItsWendell

    08/23/2021, 11:22 AM
    Yes
  • i

    ItsWendell

    08/23/2021, 11:22 AM
    I passed data through Headers instead of doing a POST request, you could also use URL params.
  • i

    ItsWendell

    08/23/2021, 11:24 AM
    Copy code
    ts
            const res = await chatroomStub.fetch(
              `http://chatroom/messageSend`,
              {
                method: 'GET',
                headers: headers,
              },
            )
    
            if (!res.webSocket) {
              throw new Error('No websockets found.')
            }
    
            res.webSocket.accept();
  • i

    ItsWendell

    08/23/2021, 11:25 AM
    I'm not sure if
    .accept
    is necessary here, but this works now (this is script from the Worker)
  • i

    ItsWendell

    08/23/2021, 11:27 AM
    Make sure you send the WebSocket back to the Worker with statusCode 101. Here's the minimized version from the DO:
  • i

    ItsWendell

    08/23/2021, 11:27 AM
    Copy code
    ts
                // Start websocket server, send client back.
                const [client, server]: WebSocket[] = Object.values(
                  new WebSocketPair(),
                )
                server.accept()
    
                // Do events on server here.
    
                return new Response(null, {
                  webSocket: client,
                  status: 101,
                });
  • m

    mz

    08/23/2021, 11:34 AM
    Thanks, but the code above is what we do, too, and: 1. Browser --- [websocket] --> Worker+DO (works in this case) 2. Worker --- [websocket] --> Worker+DO (does not work) Were you able to get websockets as in scenario #2 working?
  • i

    ItsWendell

    08/23/2021, 11:37 AM
    I'm not sure if I understand what you're trying to do. In Scenario #2, what is triggering your worker?
  • i

    ItsWendell

    08/23/2021, 11:39 AM
    Browser Worker Durable Object is how I got it working
  • i

    ItsWendell

    08/23/2021, 11:40 AM
    So there are actually 2 websocket connections, one from Browser to Worker, and one from Worker to Durable Object, and they are completely seperate, in my case the worker just proxies them though (and it could be multiple at the same time).
  • m

    mz

    08/23/2021, 11:41 AM
    The Worker itself is an originator of the WS request. What's triggering it could be a cron job or a REST call or a browser, but it doesn't forward websocket connections, but instead creates them to be sent to a Worker+DO
  • i

    ItsWendell

    08/23/2021, 11:42 AM
    Yes so like this right: Browser / Client Worker Durable Object
  • m

    mz

    08/23/2021, 11:42 AM
    Copy code
    {
      "webSocket": null,
      "url": "wss://abcdefghi.lmnopqrst.workers.dev/webSocket/createSocket",
      "redirected": false,
      "ok": false,
      "headers": {},
      "statusText": "Not Found",
      "status": 404,
      "bodyUsed": false,
      "body": {
        "locked": false
      }
    }
  • m

    mz

    08/23/2021, 11:43 AM
    No matter what we do, this is the response from DO
  • i

    ItsWendell

    08/23/2021, 11:43 AM
    You are getting a 404 here so there is something wrong in your routing there.
  • i

    ItsWendell

    08/23/2021, 11:44 AM
    If in the same handler you just do
    new Response("OK")
    it works?
  • m

    mz

    08/23/2021, 11:44 AM
    Not quite: Client / Browser --- [https] --> Worker --- [ws] --> DO
  • m

    mz

    08/23/2021, 11:45 AM
    The same code returns a websocket when a Browser is the originator of websocket requests (and worker is just proxying it through to a DO).
1...161162163...567Latest