Skip to content

ConnectedSocket DI scope for services running under WebSocketGateway #4287

Open
@IMalyugin

Description

@IMalyugin

Feature Request

Add a ConnectedSocket scope for injectables.

Is your feature request related to a problem? Please describe.

There are currently 3 types of scopes:

  • default: useful for global request unaware services, that work in stateless manner;
  • request: super useful for handling request without having to pass context manually on each method call;
  • transient: useful for... well, using the same injection mechanics for in-place class instantiation;

That works well for normal http requests, but for websockets, request scope is not a thing according to docs:
image

WebSocket service design is built upon the idea that you have a "connection" which is a lot similar to a session or request, all the data transferred between connect and disconnect is usually treated as a single scope.

Out of all available mechanics to persist that data, we have @ConnectedSocket, which is only accessible from WebSocketGateway, so without a built in support for ConnectedSocket scope, we are forced to implement our own storages within socket and pass socket around in all the calls made between WebSocketGateway and services, which is exactly why Scopes.REQUEST was initially introduced in http.

Describe the solution you'd like

Add a new ConnectedSocket scope applicable to services that would create a new instance of a provider for each connection, within such services there is a need to access current socket client same way @Request decorator does for request-scope.

As far as I understand the problem lies within the fact that we must not generate new WebSocketGateway for connections, thus the propagation mechanics of Scopes break apart. But isn't @ConnectedSocket already enforcing similar breaches? As in, it is available within WebWocketGateway, but it is running within it's own virtual "scope".

Teachability, Documentation, Adoption, Migration Strategy

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

Enforce similar system design on WebSockets as the already existing request within http eco-system to solve all the same troubles but within WebSocket context.

In addition, this would allow creating proper facades for Socket client to not call emit directly with no event/data type-checking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions