question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject Websocket into component

See original GitHub issue

I’m submitting a…


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

Current behavior

Adding these lines:

@WebSocketServer()
public socketServer: SocketIO.Server;

Results in this.socketServer being null in Components.

It would be awesome if one could get the native web socket server from everywhere. So I do not need to create a Gateway everytime I want to emit a simple message.

Expected behavior

Attribute is not null.

Minimal reproduction of the problem with instructions

@Component()
export class SocketService {
    // Only connections that are correctly authenticated get into this array
    protected authenticatedConnections: Array<SocketConnection> = [];

    // this is the SocketIO server (can be used for emit etc.)
    @WebSocketServer()
    public socketServer: SocketIO.Server;
}

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

Environment


Nest version: 4.6.7

 
For Tooling issues:
- Node version: 8  
- Platform:  

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
shekohexcommented, May 16, 2018

I was having the same issue, but i had to inject it in one gateway, that gateway dose not do anything, you can think of it as wrapper around the underlying socket server, and then i used it as a dependency in another gateways. Idk if this the only way to do this, but it works 😃

1reaction
kamilmysliwieccommented, May 16, 2018

Honestly, it’s not that easy. What if someone uses multiple gateways at the same time? IMO, you should inject a gateway to the component instead and pick up the server then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

socket.io - How to access websocket from controller or another ...
I have a REST API, I want to send event to the client via websocket. How to inject websocket instance in controller or...
Read more >
Using WebSockets with React - DEV Community ‍ ‍
For my last project, I had to use Websockets to create a website that displays real-time trading data. I didn't know anything about ......
Read more >
Connecting to Websocket Server from React - YouTube
This tutorial will teach you how to connect to a NestJS websocket server from a React application. We will be creating a React...
Read more >
Using WebSockets in Your React/Redux App | Pluralsight
WebSockets are a convenient way to create a long-running connection between a server and a client. In an instance where a web client...
Read more >
Gateways | NestJS - A progressive Node.js framework
This section covers the aspects of Nest that are specific to WebSockets. ... Hint Gateways can be treated as providers; this means they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found