Channel Decorator

Channel Decorator#

This decorator defines which channel nestjs-pusher should send the event to. It also accepts a channel builder which passes you the request parameters and allow's you to resolve the final channel endpoint

Basic example#

import { Controller, Get } from '@nestjs/common';
import { PusherChannel } from 'nestjs-pusher';
@Controller('cats')
export class CatsController {
constructor(private readonly catService: CatService) {
//Imaginary service for the sake of the example
}
@PusherChannel('private-channels-of-cats')
@Post()
async createCat(@Body() dto): string {
const newCat = await this.catService.create(dto)
//The return value will be used as the content
return newCat
}
}

Multiple channels#

import { Controller, Get } from '@nestjs/common';
import { PusherChannel } from 'nestjs-pusher';
@Controller('cats')
export class CatsController {
constructor(private readonly catService: CatService) {
//Imaginary service for the sake of the example
}
@PusherChannel(['private-channels-of-cats', 'cats-blog', 'etc etc'])
@Post()
async createCat(@Body() dto): string {
const newCat = await this.catService.create(dto)
//The return value will be used as the content
return newCat
}
}

Channel factory#

NOTE You can pass an inline function, but I'll pass a reference so this example can be easily understandable

import { Controller, Get } from '@nestjs/common';
import { PusherChannel } from 'nestjs-pusher';
const catCreatedChannelBuilder = (req: RequestInterface, eventName: string) => {
//You can perform whatever logic you want, the request is injected as the 1st parameter
return eventName + '-final'
}
@Controller('cats')
export class CatsController {
constructor(private readonly catService: CatService) {
//Imaginary service for the sake of the example
}
@PusherChannel(catCreatedChannelBuilder)
@Post()
async createCat(@Body() dto): string {
const newCat = await this.catService.create(dto)
//The return value will be used as the content
return newCat
}
}