T
tRPC

Not able to implement websockets with trpc

Not able to implement websockets with trpc

Aacronie7/7/2023
import { applyWSSHandler } from "@trpc/server/adapters/ws";
import ws from "ws";
import { createTRPCContext } from "./api/trpc";
import { appRouter } from "./api/root";

const wss = new ws.Server({
port: 3001,
});
const handler = applyWSSHandler({
wss,
router: appRouter,

createContext: createTRPCContext,
});

wss.on("connection", (ws) => {
console.log(`➕➕ Connection (${wss.clients.size})`);
ws.once("close", () => {
console.log(`➖➖ Connection (${wss.clients.size})`);
});
});
console.log("✅ WebSocket Server listening on ws://localhost:3001");

process.on("SIGTERM", () => {
console.log("SIGTERM");
handler.broadcastReconnectNotification();
wss.close();
});
import { applyWSSHandler } from "@trpc/server/adapters/ws";
import ws from "ws";
import { createTRPCContext } from "./api/trpc";
import { appRouter } from "./api/root";

const wss = new ws.Server({
port: 3001,
});
const handler = applyWSSHandler({
wss,
router: appRouter,

createContext: createTRPCContext,
});

wss.on("connection", (ws) => {
console.log(`➕➕ Connection (${wss.clients.size})`);
ws.once("close", () => {
console.log(`➖➖ Connection (${wss.clients.size})`);
});
});
console.log("✅ WebSocket Server listening on ws://localhost:3001");

process.on("SIGTERM", () => {
console.log("SIGTERM");
handler.broadcastReconnectNotification();
wss.close();
});
I have something like this atp in very early stage of trying websocket this is how my server looks rn. The connection isn't started for some reason on the server 🤔
Aacronie7/7/2023
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === "development" ||
(opts.direction === "down" && opts.result instanceof Error),
}),
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
wsLink({
client: wsClient,
}),
],
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === "development" ||
(opts.direction === "down" && opts.result instanceof Error),
}),
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
}),
wsLink({
client: wsClient,
}),
],
this is how it's lookijng the next client Anyone 😅 Subscription error: TRPCClientError: Subscriptions should use wsLink getting this error
Ppjnicolas7/25/2023
I'm a little late, but try using this links:
links: [
splitLink({
condition: (op) => op.type === 'subscription',
true: wsLink({ client: wsClient }),
false: httpBatchLink({ url: `${getBaseUrl()}/api/trpc` }),
}),
],
links: [
splitLink({
condition: (op) => op.type === 'subscription',
true: wsLink({ client: wsClient }),
false: httpBatchLink({ url: `${getBaseUrl()}/api/trpc` }),
}),
],

Looking for more? Join the community!

T
tRPC

Not able to implement websockets with trpc

Join Server
Recommended Posts
type error```TS2322: Type '{ name: string; bio: string; location: string; website: string; }' is not assignablIs there a way to use a more updated version of React Query in tRPC?I'm trying to make sure that my tRPC is using the most up to date version of v4 but I don't know howExpress + tRPC: handle express errors from clientI currently have Express as backend and react on frontend. I want to migrate gradually to tRPC + requeries running on server even though ssr is set to falseQueries are running on server side only despite the fact of setting ssr:false. Besides that, the queTypeError: client[procedureType] is not a functionHas anyone encountered this error before? I'm trying to implement an adapter for Auth.js that can coTRPC -useInfiniteQuery() refreshes all data when an input is varied, how to use?So I have a TRPC infiniteQuery: ``` const { data, fetchNextPage, hasNextPage } = trpc.server.Cannot get server response headers in browserHi, I am working on retrieving a specific server-sent header using a custom link with tRPC. This meteventemitter not having any listeners on websocketHi, I have a app router project with TRPC but I created a websocket server alongside and a websocketRPC + cls-hookedHi, im trying to implement `cls-hooked` library with trpc's protectedProcedure, but it doesnt seem ttrpc + fastify POST request not workingI have a trpc mutation like this: ``` create: publicProcedure .input(z.object({ title: z.stringproblem with useContext (likely easy fix)```tsx function userCard( user: { id: string; name: string | null; image: string | nulI think i found an TRPC BUGI am using TRPC With Fastify and bun (node.js alternative) if you try to call any .mutation route thuseMutation in useEffect dependency array causes infinite loopI had a very unexpected issue today when testing out some code. I have a simple `sum` method in tRPChow does batching work?My understanding is that batching basically combines multiple requests in a single network request,