npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

homing-courrier

v1.0.3

Published

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

Downloads

4

Readme

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