Socket Id Decorator

Socket Id Decorator#

This decorator is needed when you want to specifiy where nestjs-pusher should fetch your socket id to exclude from events. You can read more about socket id in https://pusher.com/docs/channels/server_api/excluding-event-recipients

Header name (default = x-pusher-sid)#

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

Factory#

In this example we'll be reading from the body of the request in a field named "socketId" but you can read from anywhere in the request (typically its advisable to come in a custom header)

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