title: Nest.js How to use Kafka consumer with WebSocket
body: <p>I have a microservice in nest.js where I have a kafka consumer. I need that after my consumer listens for the new message in @MessagePatter (in controller), then it emits a socket event. I have a @WebSocketServer (), but if I inject my socket server into the controller this does not generate the message.
  Any ideas, how to issue a message with socket after receiving a message with kafka in nest.js?</p>
  <p>My webSocket</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    import {
        SubscribeMessage,
        WebSocketGateway,
        OnGatewayInit,
        WebSocketServer,
        OnGatewayConnection,
        OnGatewayDisconnect,
        MessageBody,
        ConnectedSocket,
      } from '@nestjs/websockets';
      import { Logger } from '@nestjs/common';
      import { Socket, Server } from 'socket.io';
  
  
      @WebSocketGateway(89, { transports: ['websocket'] })
      export class EventsGatewayCon
        implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
      {
        @WebSocketServer() server: Server;
  
        private logger: Logger = new Logger('EventsGateway');
  
        @SubscribeMessage('testSocket')
        async testSocket(
          @ConnectedSocket() client: Socket,
          @MessageBody() payload: any,
        ): Promise&lt;void&gt; { 
          console.log(payload);
        }
  
        afterInit(server: Server) {
          this.logger.log('Init');
        }
  
        handleDisconnect(client: Socket) {
          this.logger.log(`Client disconnected: ${client.id}`);
        }
  
        handleConnection(client: Socket, ...args: any[]) {
          let theUid = null;
          if (client.handshake.query['isClient']) {
            theUid = client.handshake.query['uidClient'];
         this.server.sockets.emit('testSocket', 'new message');
          client.join(theUid);
          this.logger.log(`Client connected: ${client.id}`);
        }
      }</code></pre>
  </div>
  </div>
  </p>
  <p>Controller. Listen kafka messages</p>
  <pre class="lang-js prettyprint-override"><code> import { Controller } from '@nestjs/common';
  import { MessagePattern, Payload } from '@nestjs/microservices';
  import { AppService } from './app.service'; 
  import { EventsGatewayCon } from './events/events.gateway';
  
  @Controller()
  export class AppController {
    constructor(
      private readonly appService: AppService,
      private readonly event: EventsGatewayCon,
    ) {}
  
    @MessagePattern('myListener') // Our topic name
    async getTest(@Payload() message) {
      console.log(message.value);
      this.event.server.sockets.emit('testSocket', 'send new messa');
      return 'I am ApiKafka'; //return the message but dont emit the message with socket
    }
  }
  </code></pre>
  
  <p>AppModule</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { Module } from '@nestjs/common'; 
  import { EventsController } from './events.controller';
  import { EventsGatewayCon } from './events.gateway';
  
  @Module({
    imports: [],
    controllers: [EventsController],
    providers: [EventsGatewayCon],
    exports: [EventsGatewayCon],
  })
  export class EventsModule {}</code></pre>
  </div>
  </div>
  </p>