1.0.3 • Published 3 years ago

homing-courrier v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

HomingCourrier

A library that sits between your frontend application and a websocket server to listen to, manage and dispatch messages on demand.

Setup steps

  • npm i
  • npm run build - to build the library and output it to the lib folder
  • npm run test - to run unit tests

Approach

The aim of this library is to be used by frontend applications that wish to receive data on subscription to rooms from websockets. The HomingCourrier does that.

HomingCourrier

  • First you instantiate HomingCourrier and pass it an optional parameter of websocket endpoint
  • Then you can call one of the 4 public methods on HomingCourrier to subscribe or unsubscribe from rooms and events
  • The HomingCourrier receives subscription requests directly from the frontend applications, stores and manages the callbacks associated with each subscription to call them later, and it instantiated an instance of a WebsocketMessenger

WebsocketMessenger

  • A separate entity that's single responsibility is to open a connection with a websocket server, listen to incoming data and pass them to the HomingCourrier
  • Although the WebsocketMessenger is instantiated in the constructor of the HomingCourrier, a connection to the websocket server is only established when the first request to subscribe to a room is made by calling subscribeToRoom
  • Once the websocket server dispatches data to the WebsocketMessenger, WebsocketMessenger will notify the HomingCourrier with the room name and data by calling the callback passed by it at the instantiation level
  • You can also subscribe and unsubscribe from rooms on-demand

Progress

The following were the original requiremenets, and next to each is my progess

  • There should be only ONE connection to the service. Done ✅

  • The module should allow a frontend application to subscribe and unsubscribe from rooms on-demand. Done ✅

  • The module should emit messages, as they're received, to any subscribers. Done ✅

  • The module should only be connected to the service when there are open subscriptions. Partially done, as currently it only connects to the websocket server when the first subscription is made but I didn't have time to close the connection when all subscriptions have been removed

If I had more time

Assumptions

  • I assumed that room names are predefined but this can be made more dynamic in the future
  • Add a check to make sure we don't send a subscription request to an already subscribed-to room as I assumed we want one subscription per room
  • As this is a library, I added the ability to subscribe to helper events so that the frontend clients can diagnose errors/unexpected behaviour

Handling edge cases

  • Add a retrying logic to attempt to reconnect to the Websocket server in case the server was down
  • More unit tests to handle edge cases
  • Better error handling/logging in cases like this if (!this.ws.readyState) in ws-messanger.ts where the connection might not be ready yet and we might miss subscribing to the room. Would be nice to also add a retrying capability

Approach

  • Find a better way to mock the websocket server rather than relying on promises in ws-messanger.spec.ts like maybe using this tool jest-websocket-mock
  • Add eslint
  • Publishing the library to an npm registry
  • Build a pipeline to automate testing and publishing library using jenkins or github actions
  • Clean up logs that appear when running unit tests as atm they look like errors in the console
  • Nice to have: Build a simple demo app to visualise testing