1.0.1 • Published 3 years ago
jotive-pubsub-module v1.0.1
Jotive Nestjs PubSub Module
Nesjs module for websocket pubsub
Needs the following environment variables:
- (optional) process.env.REDIS_HOST && process.env.REDIS_PORT && process.env.REDIS_DB
- (optional) process.env.PRODUCT_NAME (for redis key prefix)
Usage in a entity module
Import module
import { PubSubModule } from '@jotive/jotive-pubsub-module';
//...
@Module({
imports: [
PubSubModule,
//...
],
//...
})
add a constant ts file with pubsub.ts postfix
export const EXPORT_CHANGES = 'exportChanges';
add to resolver a subscription
import { Args, Subscription } from '@nestjs/graphql';
import { EXPORT_CHANGES } from './export-changes.pubsub';
import { PubSubRef } from '@jotive/jotive-pubsub-module';
constructor(
@Inject(PubSubRef) private readonly pubSub: PubSub,
) { }
@Subscription(returns => String, {
filter: (payload: any, variables: any) => {
const result = payload.exportChanges === variables.projectId;
return result;
},
resolve: (payload: any, args: any, context: any, info: any) => {
return args.projectId as any;
},
})
exportChanges(@Args({ name: 'projectId', type: () => ID }) projectId: string) {
return this.pubSub.asyncIterator(EXPORT_CHANGES);
}
and use the following in the service
import { PubSub } from 'apollo-server-express';
import { PubSubRef } from '@jotive/jotive-pubsub-module';
constructor(
@Inject(PubSubRef) private readonly pubSub: PubSub,
) { }
await this.pubSub.publish(EXPORT_CHANGES, { exportChanges: projectId });
1.0.1
3 years ago