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
Neto
Neto2y ago
trpc is only for typesafe communication
cje
cje2y ago
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
not a robot
not a robot2y ago
ya that was what I was looking at but I was confused on how to set up the vanilla client in create-t3-app
cje
cje2y ago
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.
cje
cje2y ago
or you can just have something hit any endpoint
not a robot
not a robot2y ago
is their something else I'm supposed to configure?
Neto
Neto2y ago
you should check the createContext function may help
not a robot
not a robot2y ago
this one?
not a robot
not a robot2y ago
not a robot
not a robot2y ago
just the default create-t3-app one
Neto
Neto2y ago
probably ({session: null}) may work
not a robot
not a robot2y ago
wouldn't that not be typesafe?
Neto
Neto2y ago
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
not a robot
not a robot2y ago
that doesn't seem to fix it
Neto
Neto2y ago
if you need the session on the context you have to pass it on the createCaller
Neto
Neto2y ago
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.
not a robot
not a robot2y ago
oh got you
Neto
Neto2y ago
if you really need both worlds you probably will stub the info on the session just to ensure
not a robot
not a robot2y ago
"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?
Neto
Neto2y ago
you dont stub prisma just go where the createContext function is created export the prisma import on the file and pass to the createCaller
not a robot
not a robot2y ago
Ok thank you so much that did it
Neto
Neto2y ago
np
not a robot
not a robot2y ago
Neto
Neto2y ago
yep looks good just make sure to never use the session on the caller because it is null
not a robot
not a robot2y ago
yep I just want to call a function that require no session every x minutes Ok I got it from here thanks
Neto
Neto2y ago
gl
not a robot
not a robot2y ago
quick question but is publicProcedure safe to use? like would anyone be able to run it?
Neto
Neto2y ago
publicProcedure is just a middleware that doesnt check for the user does not matter for the cron
not a robot
not a robot2y ago
so someone wouldn't be able to just call an api endpoint and run it?
Neto
Neto2y ago
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
not a robot
not a robot2y ago
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
not a robot
not a robot2y ago
Neto
Neto2y ago
yes if you even need a user on the session to execute the cron there is a huge issue
not a robot
not a robot2y ago
wait but I don't need a user to run the function, I'm confused why I have to pass it in
Neto
Neto2y ago
the context requires the session
not a robot
not a robot2y ago
I just did {session: null} though
Neto
Neto2y ago
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
Neto
Neto2y ago
not a robot
not a robot2y ago
so publicProcedure is ok since I don't need a user on this function?
Neto
Neto2y ago
yes
not a robot
not a robot2y ago
ok thank you
Neto
Neto2y ago
publicProcedure only means that you dont check the user on the middleware the red part would be the protectedProcedure
not a robot
not a robot2y ago
got it thanks
Want results from more Discord servers?
Add your server