missing CF_AMBIENT_ENV
Any idea what might cause this? Just trying Rivet out for the first time, with cloudflare.
- rivet actor and worker version: 0.9.8
- @cloudflare/vite-plugin: 1.11.2
- wranger: 4.28.1
- workerd: 1.20250803.0
- miniflare: 4.20250803.0
28 Replies
Hey, thanks for using Rivetkit! Do you have perhaps any example repo to check out?
it's private, but let me try and extract out into a minimal example. probably should have done that first anyways - we'll see if I can reproduce 😅
Alrighty, it does reproduce in minimal environment. I repurposed a repo I used previously for a separate wrangler bug, and just pushed this reproduction onto the
rivet-issue branch -> https://github.com/marbemac/wrangler-sse-abort-bug/tree/rivet-issue
Can:
- clone it down
- yarn
- yarn dev
- visit http://localhost:5173/debug/actor to see the issueGitHub
GitHub - marbemac/wrangler-sse-abort-bug at rivet-issue
Contribute to marbemac/wrangler-sse-abort-bug development by creating an account on GitHub.
oh, looking at
packages/drivers/cloudflare-workers/src/handler.ts from rivet codebase, i think i see my issue actually
and i'm missing the ActorHandler export. ok one sec don't worry about this for the moment don't want to take your time - i'll try a few things and let reply back here if I still can't figure it out!thanks! sure, keep me posted!
yeah so we're not using Hono - and have our own worker handler already soooo, looking at the cloudflare handler source, seems like we'd need to run our handler with the CF_AMBIENT_ENV async storage ctx?
i guess high level is there a way today to work with rivet actors within the context of an existing (non hono based) cf worker? as I am trying to do here, trying to work w the rivet registry in this plain old CF worker -> https://github.com/marbemac/wrangler-sse-abort-bug/blob/rivet-issue/worker/index.ts#L39-L44
Side note re asynclocalstorage - w the (kinda) new support in CF for importing the env instead of having to pass it around - e.g.
import { env } from "cloudflare:workers";, could maybe get rid of need for async local storage altogether?
And take approach similar to cloudflare actor lib -> https://github.com/cloudflare/actors/blob/main/packages/core/src/index.ts#L402
Then as far as I can tell what I'm trying to do should "just work" - no need for wrapping handlers etc if not using studio atm.Thanks, checking this right now!
i pulled down the rivet repo and am trying it out now - if this seems interesting (and ends up working well) i could open a PR if you're open to it. i don't think there would be any API surface changes
ofcourse! happy to help
one issue that I spotted right away, is that you're not exporting ActorHandler which is required to start the rivetkit server.
I see you mentioned it, but the code in the repo lacks it
if its possible, you can use raw http/raw ws handlers from the rivetkit: https://www.rivet.gg/docs/actors/fetch-and-websocket-handler
Rivet
Fetch and WebSocket Handler - Rivet
Actors can handle HTTP requests and WebSocket connections through the onFetch and onWebSocket handlers.
to wrap your logic, but if you're looking for a way around - using the rivetkit inside your already created app on cf workers, then give me some time and I'll figure something out
the problem is not only in the CF_AMBIENT_ENV, but also in not running properly the code that's required for rivetkit to run properly
hey, opened a pr with a fix 🙂
let me know if it works for you!
https://github.com/marbemac/wrangler-sse-abort-bug/pull/1
GitHub
refactor: export ActorHandler, use cf-workers lib without ambient e...
This pull request updates the Cloudflare Worker integration to use the new ActorHandler durable object and updates the dependency to a custom package version. The changes modernize the worker&#...
checking it out!
btw your case its a great resource to put into our examples! lemme borrow it 😄
please do!
and yeah w the removal of the async local storage stuff (and fixing the wrangler file + exporting the DO, thanks 😅 ), works great.
hopefully nice little simplification in the base rivet cloudflare lib as well
question re https://github.com/marbemac/wrangler-sse-abort-bug/pull/1/files#diff-ed9b82a41eafb2916053294532595ab1637a3e2564673da23e9f8985f4d93a36R39 - is this needed for some reason? as far as I can tell, it's not
2nd q, just to make sure I understand (this is literally day 1 with rivet ha) - the
return handler.fetch(request, env, ctx); at the end is just for rivet studio support, or is it needed for another reason?for now, its not required to spread, but it might change it the future, ref: https://github.com/rivet-gg/rivetkit/blob/679bbde6fd24dd545975c520b12202af50b2c154/packages/drivers/cloudflare-workers/src/handler.ts#L83-L87
GitHub
rivetkit/packages/drivers/cloudflare-workers/src/handler.ts at 679b...
🧰 The open-source alternative to Durable Objects. Easily self-hostable and works with your infrastructure. - rivet-gg/rivetkit
not only! its required to run a registry, i.e. apis to spawn new actors (same stuff as
client.*) if its not required, you can skip this part!
and, if you're using our React integration, or client sdk, its also required1ah ok got it - yeah atm we'd be managing actors via registry on the backend, similar to interacting with DOs - at least for now. using orpc for all the client<->server stuff and the frontend integration
thank you for your help w this, excited to dig into rivet
no worries, happy to help!
you should definietly check out the studio! if you didnt!
Studio Token is not set, but Studio is enabled. Please set it in the run configurationabout that! docs indicate "By default, Rivet Studio generates and stores a token automatically" - but getting this logged out. Is it that it doesn't do it automatically when using CF driver?inspector.tokenor viaRIVETKIT_STUDIO_TOKENenvironment variable.
you can enable it by directly specifying in the config: https://github.com/rivet-gg/rivetkit/blob/main/packages/core/src/registry/run-config.ts#L38
i will look into why its not enabled by default
GitHub
rivetkit/packages/core/src/registry/run-config.ts at main · rivet-...
🧰 The open-source alternative to Durable Objects. Easily self-hostable and works with your infrastructure. - rivet-gg/rivetkit
seems that it IS enabled - just saying there is no token (rather than auto generating a token)
i'll set one manually
alrighty i tried to get studio setup here -> https://github.com/marbemac/wrangler-sse-abort-bug/blob/rivet-issue/worker/index.ts#L44-L52
got passed the cors issues (note for those using vite, have to configure it at viteConfig level -> https://github.com/marbemac/wrangler-sse-abort-bug/blob/rivet-issue/vite.config.ts#L10)
but run into this error log when trying to connect from the studio UI
msg="internal error" error="inspector not supported on this platform" stack="Invariant Violation: inspector not supported on this platformthanks, will look into it then!
on that last one... it's not working, no idea why I didn't change anything, but great 🙂
ah nvm - able to get to this screen but no actors showing (even though I'm refreshing
http://localhost:5173/debug/actor and getting good response back in another window)
ah we soft launched a v2 of our studio recently, cf workers is the last platform we need to impl. we’ll get that done.
apologies for the confusion
yep, apologies!
will try to fix ship it eod
no worries! can continue exploring rivet without it - from what I can tell on the empty UI, does look pretty dope though 🤓