Closed M3kH closed 5 months ago
@M3kH I'm actually working on one for a shared worker, are you still interested?
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
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?