Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: Websockets #1545

Closed
wants to merge 55 commits into from
Closed

Experiment: Websockets #1545

wants to merge 55 commits into from

Conversation

RobinTail
Copy link
Owner

@RobinTail RobinTail commented Feb 3, 2024

Exploring the possibilities based on the discussion: #991

In the end I moved it to a separate repo: https://github.com/RobinTail/zod-sockets

Concept

  • Using socket.io library as on optional peer dependency for the best quality service including acknowledgements (I believe it's an important feature).
  • Sockets is an opt-in feature by applying the new attachSockets() method on the httpServer or the httpsServer returned by the createServer() method;
  • Incoming events (from a client) are handled by the entities called Actions having validation schemas for the incoming payload and the outgoing acknowledgement (optional), similar to Endpoint.
  • Those schemas, in opposite to the Endpoint, are not z.object(), but z.tuple().
  • The returns of the Action's handler is the outgoing acknowledgement (optional), which must be the last argument received in a raw payload (a callback function, optional).
  • Actions are combined into an ActionsMap, a dictionary associating the incoming event names with their handlers, similar to Routing.
  • Actions are produced on an ActionsFactory (similar to the EndpointsFactory).
  • That factory is a class, the constructor of which accepts the EmissionMap — a dictionary associating the outgoing event names with their payload validation schemas (including acknowledgements, optional).
  • That makes each Action produced on the ActionsFactory aware of the available events, the payload constraints of which is ensured by the method emit() provided for the Action's handler.
  • The main attachRouting method also requires the emission for ensuring constraints on the offered features: onConnection, onDisconnect and onAnyEvent which are having the same type as Action handlers and equipped with the emit() method, allowing customizations.

Next

  • In case of success, the event maps can be documented according to AsyncAPI specification.

@RobinTail RobinTail added the enhancement New feature or request label Feb 3, 2024
@coveralls
Copy link

coveralls commented Feb 3, 2024

Coverage Status

coverage: 94.857% (-5.1%) from 100.0%
when pulling b5a5ef7 on exp-websockets
into 9240b34 on master.

@RobinTail RobinTail added the miracle Mysterious events are happening here label Feb 4, 2024
@RobinTail RobinTail mentioned this pull request Feb 4, 2024
@RobinTail
Copy link
Owner Author

At this moment it seems that the feature should probably be a separate package.

@RobinTail
Copy link
Owner Author

RobinTail commented Feb 5, 2024

Another problem may come from assumption that acknowledgement should be emitted in the end of the incoming event processing. But it may not be generic enough.

Actually, it's kinda OK, because otherwise there is no way to ensure that ack callback would be called.
Immediate acknowledgement can be achieved by using setTimeout(..., 0); return ...;

@RobinTail
Copy link
Owner Author

closing this PR in order to continue in a separate repo:
https://github.com/RobinTail/zod-sockets

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request miracle Mysterious events are happening here
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants