Closed Root-Control closed 5 years ago
Please, use StackOverflow for such questions.
@Root-Control did you manage to make it work? can you share your code if so?
@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
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.
I'm submitting a...
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