21Vic
Using tRPC for server to server requests
Hey everyone! The start up I’m at recently deployed a server separate from our main server. Our main server uses a combination of tRPC and Express endpoints on Node 16.14.0. When we introduced this separate server we set up some functions to make requests from our main server to the separate server.
The core of these functions is encrypting + compressing requests to the separate server as well as decrytping + decompressing on the separate server and then again on the way back (the response).
This was setup with Express on both ends, but now we’re wishing we had done it with tRPC! I saw this post: https://github.com/trpc/trpc/discussions/1566, and it gave me some reassurance about setting up a client on our main server to send requests over to the separate server.
Setting up the initial router on the separate server was no problem. But once I started thinking about how I would implement the decryption, encryption, compression and decompression logic on both ends I started getting worried about the type safety while this is at play.
I see data transformers here, https://trpc.io/docs/server/data-transformers, but I was unsure whether this could be repurposed for my use case and if the type safety would still
hold. I was also a little unsure as to what my types in defining the serialize and deserialize functions would look like.
On the other hand I thought maybe using middleware or reusable procedures would
be an option, and have the input for the tRPC procedures be encrypted and compressed already - but that clearly does away with type-safety.
Since this problem feels a little more beyond my scope than usual I figured it’d be best to seek some discussion on here instead and I may learn more and come up with a solution I’m more confident in! I appreciate any commentary, and happy to provide more information where necessary. Thank you!
2 replies