Integra
Integra
TTCTheo's Typesafe Cult
Created by Integra on 10/9/2024 in #questions
SSE httpSubscriptionLink (tRCP 11 - next 14) help please :)
Hi everyone, I have turborepo with a nextjs/prisma/tRCP and I am trying to setup sse subs (based myself on tRPC doc example). Atm I have 2 subs and a Redis client based generator
export async function* createRedisSubscription({
redis,
signal,
channel,
}: {
redis: Redis;
signal?: AbortSignal;
channel: string;
}) {
const subscriber = redis.duplicate();
await subscriber.subscribe(channel);
console.log(`Subscribed to channel: ${channel}`);

try {
const handleMessage = (channelId: string, message: string) => {
if (channelId === channel) {
console.log(`Received message on channel ${channel}:`, message);
return JSON.parse(message);
}
console.log(`Ignored message from channel ${channelId}`);
return null;
};

while (!signal?.aborted) {
const message = await new Promise((resolve) => {
const messageHandler = (channelId: string, message: string) => {
const parsedMessage = handleMessage(channelId, message);
if (parsedMessage) {
resolve(parsedMessage);
subscriber.off("message", messageHandler);
}
};
subscriber.on("message", messageHandler);
});


if (message) {
yield message;
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
} finally {
console.log(`Cleaning up subscription for channel: ${channel}`);
await subscriber.unsubscribe(channel);
await subscriber.quit();
}
}
export async function* createRedisSubscription({
redis,
signal,
channel,
}: {
redis: Redis;
signal?: AbortSignal;
channel: string;
}) {
const subscriber = redis.duplicate();
await subscriber.subscribe(channel);
console.log(`Subscribed to channel: ${channel}`);

try {
const handleMessage = (channelId: string, message: string) => {
if (channelId === channel) {
console.log(`Received message on channel ${channel}:`, message);
return JSON.parse(message);
}
console.log(`Ignored message from channel ${channelId}`);
return null;
};

while (!signal?.aborted) {
const message = await new Promise((resolve) => {
const messageHandler = (channelId: string, message: string) => {
const parsedMessage = handleMessage(channelId, message);
if (parsedMessage) {
resolve(parsedMessage);
subscriber.off("message", messageHandler);
}
};
subscriber.on("message", messageHandler);
});


if (message) {
yield message;
}
await new Promise((resolve) => setTimeout(resolve, 100));
}
} finally {
console.log(`Cleaning up subscription for channel: ${channel}`);
await subscriber.unsubscribe(channel);
await subscriber.quit();
}
}
The publish is triggered programmatically by a mutation. In isolation or if used in a single route the subs seems to be working, but if I split the subscription to be used from multiple routes eg app/page - app/stations/[hostname]/page and I open these pages in multiple tabs on reload requests get stuck on pending. Can anybody please try to help me to understand where the problem can come from?
1 replies
TTCTheo's Typesafe Cult
Created by Integra on 11/17/2023 in #questions
tRPC splitLink wsLink/httpBatchLink explanation please
I am still struggling to fully understand which approach should I use for tRPC subscriptions. Currently I have a an app created with create-3t-app with websocket server and handler up and running. As in the starter example I am using eventEmitter to trigger emit an event from a mutation and subscription that listens to that event. If I conditionally use wsLink || httpBatchLink depending by the window availability, it works as aspected. If I use splitLink base on condition op.type === "subscription" the eventEmitter is trigger but the event is lost without reaching the subscription procedure. I understand that using eventEmitter is not ideal, but still I would really appreciate if anybody could explain me the logic behind this behavior
9 replies
TTCTheo's Typesafe Cult
Created by Integra on 11/16/2023 in #questions
Next-auth V5 ERR_PACKAGE_PATH_NOT_EXPORTED main, with custom nextjs server
Hello 🙂 I am using NextJS 14/tRPC/next-auth 5 beta in turborepo. For the Nextjs app I need to add a custom server to expose ws and etc. I am running the dev server with 'tsx watch src/server' but it fails with ERR_PACKAGE_PATH_NOT_EXPORTED main in nexth-auth package.json (next-auth is a dependency of a private package ). Any ideas on how to approach the problem?
2 replies
TTCTheo's Typesafe Cult
Created by Integra on 10/27/2023 in #questions
tRPC- Nextjs app directory client & server components help to understand best approach
Hello 🙂 I have used tRPC in few projects with Nextjs in the past with the page router and hoc withTrpc but I am having few problems to understand how switch to the app router. By my understanding withTrpc is not suitable for app router. In the avail tRPC examples the RootLayout is wrapping the children into a trpc context provider (client component) which by my understanding eliminates the possibility of using SCR from the beginning (which seems bit weird to me). I have found the trpc playground app-directory repo but the layout context seems still a under definition. For a production project ( will need to stably run for a few years with no major updates) which way should I go? pages router + withTrpc, app dir + client context, predisposed server context + scr? Thank you!
1 replies
TTCTheo's Typesafe Cult
Created by Integra on 1/24/2023 in #questions
how to tRPC Prisma Zod Json field
Hi 🙂 I am stuck with a mutation, in my db Model (Prisma + pg) I have a Json field (with variable schema) . How can I define the mutation input for it with Zod?
I tried to base myself on https://github.com/colinhacks/zod#json-type but still it diverges from the given Prisma.JsonValue.
I am rather new with with stack so I might miss something obvious 🙂
1 replies
TTCTheo's Typesafe Cult
Created by Integra on 1/16/2023 in #questions
tRCP can I subscribe to a query?
hi everyone! is there a way of subscribing directly to a query and or invalidate a query directly from the tRCP router? I am using create-t3-app + NextJs + Prisma (pg) Thank you in advance!
2 replies
TTCTheo's Typesafe Cult
Created by Integra on 1/16/2023 in #questions
sync realtime DB ( or query sub) to global store
Hey 🙂 I am doing some realtime experiments (nextJS + Supabase or nextJS + tRPC) . In general, have my basic queries/mutations/subs up and running but because the data can/will be used within different components I feel the need of syncing these values into a global store ( Recoil or Redux). Which way do you think it would be best? I am thinking to create a global data wrapper provider but I wonder how scalable this can be and if there are better solutions.
1 replies
TTCTheo's Typesafe Cult
Created by Integra on 1/13/2023 in #questions
tRPC NextJs Prisma Ws monorepo
Hey 🙂 I am rather new with tRPC so forgive me for the noob question. For my next project I would like to setup a monorepo with Nx with 2 NextJs apps ( admin ui & preview ui). Ideally these apps should share the tRPC server api and Ws subscriptions. Is there any documentation/example/discussion to refer to? Thank you!
4 replies