nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.89k stars 7.56k forks source link

Call socket when mongoose model is Changed WebSocketGateway #1251

Closed Root-Control closed 5 years ago

Root-Control commented 5 years ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I need to know how can i use the websockets after user crud

Expected behavior

Minimal reproduction of the problem with instructions

import { SubscribeMessage, WebSocketGateway, WebSocketServer, WsResponse, OnGatewayInit } from '@nestjs/websockets'; import { from, Observable } from 'rxjs'; import { map } from 'rxjs/operators';

@WebSocketGateway()

export class EventsGateway implements OnGatewayInit { @WebSocketServer() server;

constructor() { }

afterInit() { console.log(this.server); this.server.emit('testing', { do: 'stuff' }); }

@SubscribeMessage('testing') sendTesting(client, data) { client.emit('testing', { testing: 'works' }); } }

What is the motivation / use case for changing the behavior?

I tested injecting provider, but doesn't works

Environment


Nest version: 5.1.0


For Tooling issues:
- Node version: 8.9.0
- Platform:  Windows

Others:

kamilmysliwiec commented 5 years ago

Please, use StackOverflow for such questions.

saade commented 5 years ago

@Root-Control did you manage to make it work? can you share your code if so?

Root-Control commented 5 years ago

@Root-Control did you manage to make it work? can you share your code if so?

Yes, i created a single file for module, i.e. articles.gateway.ts (gateway for article model) This is the code:

import {
  SubscribeMessage,
  WebSocketGateway,
  WebSocketServer,
  WsResponse,
  OnGatewayInit
} from '@nestjs/websockets';
import { from, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@WebSocketGateway()

export class ArticlesGateway implements OnGatewayInit {
  @WebSocketServer() server;

  constructor() {}

  afterInit() {
  }

  sendArticlesListFromSocket(articles) {
    console.log(this.server);
    return this.server.emit('articles', { message: 'from controller' });
  }
}

and i call for controller in this way

export class ArticlesController {
  constructor(private readonly articlesService: ArticlesService, 
              private readonly articlesSocket: ArticlesGateway) {
  }

  @Get('')
  @Roles('admin')
  async list(@Req() req) {
    const articles = await this.articlesService.list();
    this.articlesSocket.sendArticlesListFromSocket(articles);
    return articles;
  }

I hope it helps

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.