@mediaedge4tw/nestjs-pubsub
v1.0.1
Published
PubSubJS for nestjs module
Readme
Description
Installation
npm i --save @mediaedge4tw/nest-pubsubQuick Start
import in AppModule
PubsubModule is a global module.
import { Module } from '@nestjs/common';
import {PubsubModule} from '@mediaedge4tw/nest-pubsub'
@Module({
imports: [PubsubModule.forRoot()],
})
export class AppModule { }call the pubsub channel in your service
import { PubTestDataDto } from './pub-test-data.dto';
import { Injectable } from '@nestjs/common';
import { InjectPubSubChannel, NestSubscriptionListener, PubsubService } from '../../../lib';
@Injectable()
export class PubTestService {
constructor(
@InjectPubSubChannel() private readonly pubsubService: PubsubService<PubTestDataDto> // channel name is Default<your-channel-name
) { }
public publish(subChannel: string, data: PubTestDataDto) {
return this.pubsubService.publish([subChannel], data);
}
public subscribe(channel: string, func: NestSubscriptionListener<PubTestDataDto>): PubSubJS.Token {
return this.pubsubService.subtribe([subChannel], func);
}
public unsubscribe(token: string) {
this.pubsubService.unsubscribe(token);
}
}you can create custom channel dynamic by custom name
@Injectable()
export class PubService {
constructor(
@InjectPubSubChannel('<your-channel-name>')
private readonly pubsubService: PubsubService<PubTestDataDto> // channel name is <your-channel-name
) { }
public publish(subChannel: string, data: PubTestDataDto) {
return this.pubsubService.publish([subChannel], data);
}
}
@Injectable()
export class SubService {
constructor(
@InjectPubSubChannel('<your-channel-name>')
private readonly pubsubService: PubsubService<PubTestDataDto> // channel name is <your-channel-name>
) { }
public subscribe(channel: string, func: NestSubscriptionListener<PubTestDataDto>): PubSubJS.Token {
return this.pubsubService.subtribe([subChannel], func);
}
public unsubscribe(token: string) {
this.pubsubService.unsubscribe(token);
}
}
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
