hadeeb / trpc-worker

Run tRPC in a web-worker
https://www.npmjs.com/package/@hadeeb/trpc-worker
15 stars 0 forks source link

Great work! You should publish it. #1

Closed M3kH closed 5 months ago

M3kH commented 1 year ago

I know that this might feel a bit hacky, since was not the original intent of tRPC.

I find this a clever way to communicate and organising web Workers. I'm already using in one of my experiments.

Thanks for the work and for sharing it.

If you could, I will recommend to publish it to npm.

One question: Why was the idea behind create a new MessageChannel? Would your library work for SharedWorker?

HansBhatia commented 10 months ago

@M3kH I'm actually working on one for a shared worker, are you still interested?

hadeeb commented 9 months ago

SharedWorker should work in theory. Something like this might work, I haven't tried.

sharedWorker = new SharedWorker()
createWorkerClient({ worker:sharedWorker.port })

In the websocket adapter, trpc "server" is long living and multiple clients connect and disconnects. When we run trpc in web worker, this dynamic changes. The "server" is the short lived process.

A MessageChannel is created to simulate a websocket connection so that we can reuse the websocket adapter & link without having to re-implement any trpc message handling