Andrés
Andrés
TtRPC
Created by Andrés on 1/13/2025 in #❓-help
Can we bundle WS TRPC Server?
Nevermind, this is an stupid question, of course you can
5 replies
TtRPC
Created by Andrés on 1/13/2025 in #❓-help
Can we bundle WS TRPC Server?
Trying with
esbuild ./src/server/wsServer.ts --bundle --outfile=bundle.cjs --platform=node --minify --external:mock-aws-s3 --external:nock
esbuild ./src/server/wsServer.ts --bundle --outfile=bundle.cjs --platform=node --minify --external:mock-aws-s3 --external:nock
but looks like something is not working.
5 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
also IDK if it's ok to do a new redis connection on every subscription
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
And I was wondering if there's a cleaner way
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
Ended up doing my notification stream like this
streamNotifications: publicProcedure.subscription(async function*() {
const subRedis = new Redis(RedisParams);
let resolveQueue:
| ((value: TNotification | PromiseLike<TNotification>) => void)
| null = null;
try {
await subRedis.subscribe("notifications");
subRedis.on("message", (_, message) => {
const notification: TNotification = SuperJSON.parse(message);
if (resolveQueue) {
resolveQueue(notification);
resolveQueue = null;
} else {
queue.push(notification);
}
});
const queue: TNotification[] = [];
while (true) {
if (queue.length > 0) {
yield queue.shift()!;
} else {
yield await new Promise<TNotification>((resolve) => {
resolveQueue = resolve;
});
}
}
} catch (error) {
console.error("Error in notification stream:", error);
throw error; // Rethrow or handle as appropriate
} finally {
console.log("Unsubscribed from notifications");
subRedis.disconnect();
}
}),
streamNotifications: publicProcedure.subscription(async function*() {
const subRedis = new Redis(RedisParams);
let resolveQueue:
| ((value: TNotification | PromiseLike<TNotification>) => void)
| null = null;
try {
await subRedis.subscribe("notifications");
subRedis.on("message", (_, message) => {
const notification: TNotification = SuperJSON.parse(message);
if (resolveQueue) {
resolveQueue(notification);
resolveQueue = null;
} else {
queue.push(notification);
}
});
const queue: TNotification[] = [];
while (true) {
if (queue.length > 0) {
yield queue.shift()!;
} else {
yield await new Promise<TNotification>((resolve) => {
resolveQueue = resolve;
});
}
}
} catch (error) {
console.error("Error in notification stream:", error);
throw error; // Rethrow or handle as appropriate
} finally {
console.log("Unsubscribed from notifications");
subRedis.disconnect();
}
}),
It works good, but it feels weird using a while(true)
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
Thanks, I was wondering if you have an example that uses an async generator
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
thanks in advance
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
Can you give me an example of implementation of this with Redis? @BeBoRE
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
Looks like the eventEmmiter that's created in the subscrition lives in the ws server and the emit event
emitFirstNotification: publicProcedure.mutation(async ({ ctx }) => {
const notifications = await ctx.db.notification.findFirstOrThrow({
select: selectnotificacion.select,
orderBy: { createdAt: "desc" },
});
console.log("Emmiting");
const emmiting = notificationEventEmitter.emit("add", notifications);
console.log(emmiting, "listeners?");
}),
emitFirstNotification: publicProcedure.mutation(async ({ ctx }) => {
const notifications = await ctx.db.notification.findFirstOrThrow({
select: selectnotificacion.select,
orderBy: { createdAt: "desc" },
});
console.log("Emmiting");
const emmiting = notificationEventEmitter.emit("add", notifications);
console.log(emmiting, "listeners?");
}),
Lives in the nextjs proccess So the event emmiter has this problem I probably could fix this by implementing a redis/valkey server but I would really like to keep the app small Also I dont understand why subscriptions are being run on the ws server, maybe is something of my setup? I would really like to know
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
It works. but I must declare the terminating link for mutations as the wsLink
condition: (op) => op.type === "subscription" || op.type === "mutation"
condition: (op) => op.type === "subscription" || op.type === "mutation"
16 replies
TtRPC
Created by Andrés on 1/7/2025 in #❓-help
Problem using EventEmitters [Weird behavior]
Its kind of funny
16 replies