-
Notifications
You must be signed in to change notification settings - Fork 9
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
[mds-web-socket] extensible entities #310
Conversation
A next step on this PR might be allowing the accepted entities to be extended or overridden somehow. Or just removing the entity checking altogether if we assume all senders are to be trusted to send the right thing (thinking out loud) |
I made a pass at the ability to override the supported events/entities withing WebSocketServer. Admittedly it's without clarity on where this needs to go, I'm playing around a bit. Essentially an event-to-entity mapping gets passed in as a function param and it is used to check incoming events and only pass on those that are present in the config. The default mapping is the equivalent to what was previously hard-coded (events, telemetries only). So if one wanted to override the default mapping with new events/entities, based on how WebSocketServer() is invoked in containers-images/mds-web-sockets/index.ts it would mean the new config would get passed in here, which I'm not sure feels right. A different approach might be to turn WebSocketServer into a class and it can be extended with different configuration. Feedback welcome @avatarneil
Type signature is With the web-socket types having been expanded to
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twelch Couple of thoughts:
We could use a generic string literal array instead of a map for the types, e.g.
const ENTITY_TYPES = ['event', 'telemetry'] as const
const ENTITY_TYPE = typeof ENTITY_TYPES[number]
This would allow us to pass in a generic at init time for the server, something like
const WebSocketServer = <T extends readonly string[]>(entityTypes: T = EntityTypes) => ...
And then we could define a "temporary" type within the server that indexes that (so we can actually pull out the entries) the same way we do with the defaults. This would allow the configuration to be automagically inferred if the client specifies the entityTypes
.
The one thing that's lost with the approach I outlined above, is that we lose the all-caps that is desired for our payload protocol, but to get around that we could just use .toUpperCase()
on the entityTypes.
The main benefit IMO is that we can drop all of the loose string
typings, and actually have the types be derived from the entityTypes
string literal array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine aside from @avatarneil suggestions.
@avatarneil toUppercase isn't quite enough, note 'TELEMETRIES' is not the uppercase of 'telemetry'. Are you proposing I should introduce 'TELEMETRY' and 'EVENT' as a breaking change to WS protocol replacing 'TELEMETRIES' and 'EVENTS'? Why is uppercase "desired"? Just noting we could avoid the transform altogether potentially. |
@twelch Ahh, right. Yeah, I think that consolidating down |
I'm now focused on generalizing the Clients class, which stores subscriptions using hardcoded entity names |
@avatarneil this is ready for another review, thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM baring the one rename comment @drtyh2o made
Merged latest goodness and made requested changes. Please see last comment @avatarneil and advise on merge timing. Thanks. |
…into feature/ws-generic
This is an initial stab at making the web socket package not so tightly coupled to base MDS VehicleEvent and Telemetry types. It allows any type of JSON to be sent. The Nats EventProcessor sending events on the other end seems to use open-ended generic typing already and the WS server code was simply casting to more specific types. Not clear to me if that gains anything over accepting Json type.
Feedback welcome on approach, and whether other parts that would need this treatment. The WS client seems unused and possibly not in need of migration.
PR Checklist
[mds-db] Add PG env var
,[config] Fix eslint config