dylan42432
dylan42432
TtRPC
Created by dylan42432 on 5/6/2025 in #❓-help
Unref() the websocket
Hello everyone, What I desire is to use a websocket link but not have TRPC hold the process open indefinitely, preventing my Node.js script from finishing (as is default when you have a websocket connection open). If you merely unref() the underlying socket then the process quits before the query/mutation finishes. The following is my working solution, but I'm wondering if there was a simpler/more elegant solution:
const ensureProcessLives: TRPCLink<AppRouter> = () => {
return ({ next, op }) => {
return observable((observer) => {
const keepAliveTimeout = setTimeout(() => {}, 2147483647);

return next(op).subscribe({
next(value) {
observer.next(value);
},
error(err) {
observer.error(err);
clearTimeout(keepAliveTimeout);
},
complete() {
observer.complete();
clearTimeout(keepAliveTimeout);
},
});
});
};
};

const wsClient = createWSClient({
WebSocket: WS as unknown as typeof WebSocket,
url: `ws://localhost:3001`,
onOpen: () => {
this.socket = wsClient['activeConnection'].ws._socket;
this.socket?.unref();
},
connectionParams: { id: this.id }
});

this.trpc = createTRPCClient<AppRouter>({
links: [
ensureProcessLives,
wsLink<AppRouter>({ client: wsClient })
],
});
const ensureProcessLives: TRPCLink<AppRouter> = () => {
return ({ next, op }) => {
return observable((observer) => {
const keepAliveTimeout = setTimeout(() => {}, 2147483647);

return next(op).subscribe({
next(value) {
observer.next(value);
},
error(err) {
observer.error(err);
clearTimeout(keepAliveTimeout);
},
complete() {
observer.complete();
clearTimeout(keepAliveTimeout);
},
});
});
};
};

const wsClient = createWSClient({
WebSocket: WS as unknown as typeof WebSocket,
url: `ws://localhost:3001`,
onOpen: () => {
this.socket = wsClient['activeConnection'].ws._socket;
this.socket?.unref();
},
connectionParams: { id: this.id }
});

this.trpc = createTRPCClient<AppRouter>({
links: [
ensureProcessLives,
wsLink<AppRouter>({ client: wsClient })
],
});
2 replies
TtRPC
Created by dylan42432 on 4/19/2025 in #❓-help
bigint
My trpc client is showing type never for bigints. How can I fix this? Thanks
2 replies