moloch
moloch
Explore posts from servers
TtRPC
Created by .westsaid on 10/12/2022 in #❓-help
How to use trpc react hooks from an external data source
Thanks for this repo, it was super helpful to me. Have you ever tried enabling tRPC's subscription support via websockets? struggling to get that working inside of Keystone.js's config
8 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
np, and good luck!
8 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
Ya I'm not sure how performant that is, but it seemed to be the only way I could get it to work when I was fiddling around
8 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
that's great though
8 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
I just used the redis npm package
8 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
I also struggled with setting up redis. My current working example is much the result of brute force trial / error:
onFact: publicProcedure.subscription(async ({ ctx }) => {
if (!redisClient.isReady) {
console.log("Redis client is not connected!");
await redisClient.connect();
}


// return an `observable` with a callback which is triggered immediately
return observable<CatFact>((emit) => {
// define a listener function to handle incoming messages
const listener = (message: string, channel: string) => {
console.log(`Received message on channel ${channel}: ${message}`);

if (channel === "cat-facts") {
const fact = JSON.parse(message);
// emit data to client
emit.next(fact);
}
};

// subscribe to the `cat-facts` channel in Redis
const subscriber = redisClient.duplicate();
subscriber.subscribe("cat-facts", listener);

subscriber.connect();

const onCatFact = (channel: string, message: string) => {
if (channel === "cat-facts") {
const fact = JSON.parse(message);
// emit data to client
emit.next(fact);
}
};
// trigger `onCatFact()` when a message is published to the `cat-facts` channel
subscriber.on("message", onCatFact);

// unsubscribe function when client disconnects or stops subscribing
return () => {
// unsubscribe from the `cat-facts` channel in Redis
subscriber.unsubscribe("cat-facts");
subscriber.off("message", onCatFact);
};
});
}),
onFact: publicProcedure.subscription(async ({ ctx }) => {
if (!redisClient.isReady) {
console.log("Redis client is not connected!");
await redisClient.connect();
}


// return an `observable` with a callback which is triggered immediately
return observable<CatFact>((emit) => {
// define a listener function to handle incoming messages
const listener = (message: string, channel: string) => {
console.log(`Received message on channel ${channel}: ${message}`);

if (channel === "cat-facts") {
const fact = JSON.parse(message);
// emit data to client
emit.next(fact);
}
};

// subscribe to the `cat-facts` channel in Redis
const subscriber = redisClient.duplicate();
subscriber.subscribe("cat-facts", listener);

subscriber.connect();

const onCatFact = (channel: string, message: string) => {
if (channel === "cat-facts") {
const fact = JSON.parse(message);
// emit data to client
emit.next(fact);
}
};
// trigger `onCatFact()` when a message is published to the `cat-facts` channel
subscriber.on("message", onCatFact);

// unsubscribe function when client disconnects or stops subscribing
return () => {
// unsubscribe from the `cat-facts` channel in Redis
subscriber.unsubscribe("cat-facts");
subscriber.off("message", onCatFact);
};
});
}),
hope this helps!
8 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
Thank you very much for the detailed response! The issue turned out to be caused by Next.js using different processes and therefor contexts between the two endpoints, I was using an EventEmitter to trigger the webhooks but each context had it's own isolated EventEmitter. Ended up switching to Redis to solve the issue 🤦‍♀️
8 replies