How to use the @nestjs/microservices.EventPattern function in @nestjs/microservices

To help you get started, weโ€™ve selected a few @nestjs/microservices examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github new-eden-social / new-eden-social / src / services / websocket / websocket.gateway.ts View on Github external
/**
   * Send event to all sockets connected to this character's wall
   */
  @EventPattern(WS_REDIS_EVENTS.CHARACTER_WALL)
  public sendEventToCharacterWallSub(payload: DWsRedisCharacterWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_CHARACTER_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToCharacterWall => ${payload.characterId} = ${event.event}`,
    );
    this.server.to(getRoomForCharacterWall(payload.characterId)).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this corporation's wall
   */
  @EventPattern(WS_REDIS_EVENTS.CORPORATION_WALL)
  public sendEventToCorporationWallSub(payload: DWsRedisCorporationWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_CORPORATION_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToCorporationWall => ${payload.corporationId} = ${event.event}`,
    );
    this.server.to(getRoomForCorporationWall(payload.corporationId)).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this alliance's wall
   */
  @EventPattern(WS_REDIS_EVENTS.ALLIANCE_WALL)
  public sendEventToAllianceWallSub(payload: DWsRedisAllianceWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_ALLIANCE_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToAllianceWall => ${payload.allianceId} = ${event.event}`,
github new-eden-social / new-eden-social / src / services / websocket / websocket.gateway.ts View on Github external
* Send event to character (All sockets for this character)
   */
  @EventPattern(WS_REDIS_EVENTS.CHARACTER)
  public sendEventToCharacter(payload: DWsRedisCharacter): void {
    const clients = this.getSocketsForCharacter(payload.characterId);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToCharacter [${clients.length}]` +
      ` => ${payload.characterId} = ${payload.event}`,
    );
    clients.forEach(socket => socket.emit(payload.event, payload.data));
  }

  /**
   * Send event to all sockets connected to latest wall
   */
  @EventPattern(WS_REDIS_EVENTS.LATEST_WALL)
  public sendEventToLatestWallSub(payload: DWsRedisLatestWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_LATEST_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToLatestSub => latest = ${event.event}`,
    );
    this.server.to(getRoomForLatestWall()).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this character's wall
   */
  @EventPattern(WS_REDIS_EVENTS.CHARACTER_WALL)
  public sendEventToCharacterWallSub(payload: DWsRedisCharacterWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_CHARACTER_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToCharacterWall => ${payload.characterId} = ${event.event}`,
github new-eden-social / new-eden-social / src / services / websocket / websocket.gateway.ts View on Github external
/**
   * Send event to all sockets connected to latest wall
   */
  @EventPattern(WS_REDIS_EVENTS.LATEST_WALL)
  public sendEventToLatestWallSub(payload: DWsRedisLatestWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_LATEST_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToLatestSub => latest = ${event.event}`,
    );
    this.server.to(getRoomForLatestWall()).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this character's wall
   */
  @EventPattern(WS_REDIS_EVENTS.CHARACTER_WALL)
  public sendEventToCharacterWallSub(payload: DWsRedisCharacterWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_CHARACTER_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToCharacterWall => ${payload.characterId} = ${event.event}`,
    );
    this.server.to(getRoomForCharacterWall(payload.characterId)).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this corporation's wall
   */
  @EventPattern(WS_REDIS_EVENTS.CORPORATION_WALL)
  public sendEventToCorporationWallSub(payload: DWsRedisCorporationWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_CORPORATION_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToCorporationWall => ${payload.corporationId} = ${event.event}`,
github new-eden-social / new-eden-social / src / services / websocket / websocket.gateway.ts View on Github external
/**
   * Send event to all sockets connected to this alliance's wall
   */
  @EventPattern(WS_REDIS_EVENTS.ALLIANCE_WALL)
  public sendEventToAllianceWallSub(payload: DWsRedisAllianceWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_ALLIANCE_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToAllianceWall => ${payload.allianceId} = ${event.event}`,
    );
    this.server.to(getRoomForAllianceWall(payload.allianceId)).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this hashtag's wall
   */
  @EventPattern(WS_REDIS_EVENTS.HASHTAG_WALL)
  public sendEventToHashtagWallSub(payload: DWsRedisHashtagWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_HASHTAG_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToHashtagWall => ${payload.hashtag} = ${event.event}`,
    );
    this.server.to(getRoomForHashtagWall(payload.hashtag)).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this post's comments
   */
  @EventPattern(WS_REDIS_EVENTS.POST_COMMENTS)
  public sendEventToPostCommentSub(payload: DWsRedisPostComments): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_POST_COMMENTS);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToPostCommentSub => ${payload.postId} = ${event.event}`,
github new-eden-social / new-eden-social / src / services / websocket / websocket.gateway.ts View on Github external
this.logger.debug(
        `[Websocket.Gateway] authenticate => ${client.characterId} = success`,
      );
      return new DWsAuthentication(true);
    } catch (e) {
      this.logger.debug(
        `[Websocket.Gateway] authenticate => ${client.id} = fail`,
      );
      return new DWsAuthentication(false);
    }
  }

  /**
   * Send event to character (All sockets for this character)
   */
  @EventPattern(WS_REDIS_EVENTS.CHARACTER)
  public sendEventToCharacter(payload: DWsRedisCharacter): void {
    const clients = this.getSocketsForCharacter(payload.characterId);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToCharacter [${clients.length}]` +
      ` => ${payload.characterId} = ${payload.event}`,
    );
    clients.forEach(socket => socket.emit(payload.event, payload.data));
  }

  /**
   * Send event to all sockets connected to latest wall
   */
  @EventPattern(WS_REDIS_EVENTS.LATEST_WALL)
  public sendEventToLatestWallSub(payload: DWsRedisLatestWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_LATEST_WALL);
    this.logger.debug(
github new-eden-social / new-eden-social / src / services / websocket / websocket.gateway.ts View on Github external
/**
   * Send event to all sockets connected to this hashtag's wall
   */
  @EventPattern(WS_REDIS_EVENTS.HASHTAG_WALL)
  public sendEventToHashtagWallSub(payload: DWsRedisHashtagWall): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_HASHTAG_WALL);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToHashtagWall => ${payload.hashtag} = ${event.event}`,
    );
    this.server.to(getRoomForHashtagWall(payload.hashtag)).emit(event.event, event.data);
  }

  /**
   * Send event to all sockets connected to this post's comments
   */
  @EventPattern(WS_REDIS_EVENTS.POST_COMMENTS)
  public sendEventToPostCommentSub(payload: DWsRedisPostComments): void {
    const event = new DWsNewSubscriptionEvent(payload.data, WS_SUBSCRIPTIONS.TO_POST_COMMENTS);
    this.logger.debug(
      `[Websocket.Gateway] sendEventToPostCommentSub => ${payload.postId} = ${event.event}`,
    );
    this.server.to(getRoomForPostComments(payload.postId)).emit(event.event, event.data);
  }

  private getSocketsForCharacter(characterId: number): ISocket[] {
    return this.clients.filter(c => c.characterId === characterId);
  }

}
github nestjs / nest / integration / microservices / src / mqtt / mqtt.controller.ts View on Github external
.send({ cmd: 'sum' }, tab)
        .toPromise();

      return result === expected;
    };
    return data
      .map(async tab => send(tab))
      .reduce(async (a, b) => (await a) && b);
  }

  @Post('notify')
  async sendNotification(): Promise {
    return this.client.emit('notification', true);
  }

  @EventPattern('notification')
  eventHandler(data: boolean) {
    MqttController.IS_NOTIFIED = true;
  }

  @MessagePattern({ cmd: 'sum' })
  sum(data: number[]): number {
    return (data || []).reduce((a, b) => a + b);
  }

  @MessagePattern({ cmd: 'asyncSum' })
  async asyncSum(data: number[]): Promise {
    return (data || []).reduce((a, b) => a + b);
  }

  @MessagePattern({ cmd: 'streamSum' })
  streamSum(data: number[]): Observable {
github nestjs / nest / integration / microservices / src / redis / redis.controller.ts View on Github external
@MessagePattern({ cmd: 'streamSum' })
  streamSum(data: number[]): Observable {
    return of((data || []).reduce((a, b) => a + b));
  }

  @MessagePattern({ cmd: 'streaming' })
  streaming(data: number[]): Observable {
    return from(data);
  }

  @Post('notify')
  async sendNotification(): Promise {
    return this.client.emit('notification', true);
  }

  @EventPattern('notification')
  eventHandler(data: boolean) {
    RedisController.IS_NOTIFIED = data;
  }
}
github nestjs / nest / integration / microservices / src / rmq / rmq.controller.ts View on Github external
@MessagePattern({ cmd: 'streamSum' })
  streamSum(data: number[]): Observable {
    return of((data || []).reduce((a, b) => a + b));
  }

  @MessagePattern({ cmd: 'streaming' })
  streaming(data: number[]): Observable {
    return from(data);
  }

  @Post('notify')
  async sendNotification(): Promise {
    return this.client.emit('notification', true);
  }

  @EventPattern('notification')
  eventHandler(data: boolean) {
    RMQController.IS_NOTIFIED = data;
  }
}
github nestjs / nest / integration / microservices / src / nats / nats.controller.ts View on Github external
return this.client
      .send('exception', {})
      .pipe(catchError(err => of(err)));
  }

  @MessagePattern('exception')
  throwError(): Observable {
    return throwError(new RpcException('test'));
  }

  @Post('notify')
  async sendNotification(): Promise {
    return this.client.emit('notification', true);
  }

  @EventPattern('notification')
  eventHandler(@Payload() data: boolean) {
    NatsController.IS_NOTIFIED = data;
  }
}