Skip to content

Instantly share code, notes, and snippets.

@musou1500
Created July 3, 2021 11:13
Show Gist options
  • Save musou1500/d20f2a9e36c73ebf73fcbb3f09fa12c5 to your computer and use it in GitHub Desktop.
Save musou1500/d20f2a9e36c73ebf73fcbb3f09fa12c5 to your computer and use it in GitHub Desktop.
Example nestjs-graphql-redis-subscriptions
import { Inject, ValidationPipe } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { Module } from "@nestjs/common";
import {
IOModule,
RedisPubSub,
REDIS_PUB_SUB_TOKEN,
} from "nestjs-graphql-redis-subscriptions";
class AppService {
constructor(@Inject(REDIS_PUB_SUB_TOKEN) private pubsub: RedisPubSub) {}
publish(trigger: string, payload: unknown): Promise<void> {
return this.pubsub.publish(trigger, payload);
}
}
@Module({
controllers: [],
providers: [AppService],
imports: [
IOModule.registerRedisPubSub(
// publisher connection
{
useFactory: () => ({
port: 6379,
host: "127.0.0.1",
}),
},
// subscriber connection
{
useFactory: () => ({
port: 6379,
host: "127.0.0.1",
}),
}
),
],
})
export class AppModule {}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({ transform: true }));
await app.listen(3000);
}
bootstrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment