How to make a trpc function run automatically on start-up of server.
I'm working on a RSS reader like-app and am try to have function that runs every X minutes to refresh the feeds but can't seem to figure out how to go about this in trpc. Anyone have any suggestions?
43 Replies
trpc is only for typesafe communication
you're looking for cronjobs
if you're hosting on vercel or another serverless provider you'll need an external service for this because there is no "always on" server
ya that was what I was looking at but I was confused on how to set up the vanilla client in create-t3-app
Server Side Calls | tRPC
You may need to call your procedure(s) directly from the server, createCaller() function returns you an instance of RouterCaller able to execute queries and mutations.
or you can just have something hit any endpoint
is their something else I'm supposed to configure?
you should check the createContext function
may help
this one?
just the default create-t3-app one
probably ({session: null})
may work
wouldn't that not be typesafe?
the session is more about auth user
if you add prisma later on the context, you probably will need to pass the prisma on the createCaller too
that doesn't seem to fix it
if you need the session on the context
you have to pass it on the createCaller
Server Side Calls | tRPC
You may need to call your procedure(s) directly from the server, createCaller() function returns you an instance of RouterCaller able to execute queries and mutations.
oh got you
if you really need both worlds
you probably will stub the info on the session just to ensure
"Property 'prisma' is missing in type '{ session: null; }' but required in type '{ session: Session | null; prisma: PrismaClient<Prisma.PrismaClientOptions, never, Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined>; }'."
How would I stub prisma?
you dont stub prisma
just go where the createContext function is created
export the prisma
import on the file
and pass to the createCaller
Ok thank you so much that did it
np
yep
looks good
just make sure to never use the session on the caller
because it is null
yep I just want to call a function that require no session every x minutes
Ok I got it from here thanks
gl
quick question but is publicProcedure safe to use? like would anyone be able to run it?
publicProcedure is just a middleware that doesnt check for the user
does not matter for the cron
so someone wouldn't be able to just call an api endpoint and run it?
the cron should need a user on session to be executed
with the createCaller you are just executing router functions without a external source
like from next -> trpc -> route function
so would I need to pass a user into this because what it does doesn't require any users and just updates the feeds for all users
yes
if you even need a user on the session to execute the cron
there is a huge issue
wait but I don't need a user to run the function, I'm confused why I have to pass it in
the context requires the session
I just did {session: null} though
yes
the createCaller needs the Context contract to run correctly
but you dont need the user that could be populated when the call is made from the client side
so publicProcedure is ok since I don't need a user on this function?
yes
ok thank you
publicProcedure only means that you dont check the user on the middleware
the red part would be the protectedProcedure
got it thanks