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

primus-turborooms

v1.0.1

Published

Basic rooms extension for Primus with focus on Performance / Efficiency

Downloads

5

Readme

Turborooms - Basic rooms extension for Primus with focus on Performance / Efficiency

Lore

Primus, in itself, is a great project however there was one thing bugging me about it: A lot of unused potential in terms of performance because of its compatability with many transformers and de-/serializers.

I for my part always use the µWs transformer which in itself already has an edge in performance over the other transformers, but it also offers methods to prepare and then send that already prepared message to multiple clients. Primus-rooms could make use of that but I suppose it doesnt to have the code be in-line with the other transformers / documented endpoints: https://github.com/cayasso/primus-rooms-adapter/blob/4e3cbd492313f52cb0d2c0ef43589a7b7c41a3f6/lib/index.js#L200

However, it has another major flaw: Since the message is sent to every recipient trough the documented interfaces, The Serializer gets called for every single client that you try to submit to: https://github.com/primus/primus/blob/f54a102cb088a8c9e0b629624fbe72f6d76d05ff/spark.js#L441

This obviously massively hinders troughput as the same message has to get encoded over and over again for every client that will receive it. This is neither really a fault of Primus, nor is it one of Primus-rooms, its merely something that I felt I could improve on for my exact use-case, and thought I might as well share it :)

Usage

Things to keep in mind:

  • Sparks are not automatically removed from the room(s) they are in. You need to manually handle a sparks end event and call the .leave(roomname) method for every room that the spark either, is in, or all the rooms it could be in. With my use-case the rooms a spark could be in are known / available from the end event (Simply added onto the Spark object) hence I made this decision so I dont have to maintain a second spark: rooms relation. Failing this step almost certainly will cause memory leaks and / or weird behaviour.
  • Turborooms uses some undocumented, and some privately marked endpoints, so it may or may not break with future versions.
  • Turborooms is very barebones by itself, however has included support for the First-party Primus-Emit plugin: https://github.com/primus/primus-emit
  • You can use Turborooms with any transformer, not just µWs, other transformers are going to miss out on its improvements. If you wish to use a different transformer other than µWs add "turborooms: {ignoreNoUws: true}" to your Primus options parameter when initializing.
  • This is merely implementing the things that I needed for my usecase and might get expanded over time. If you miss something that could be added onto it without decreasing performance of anything thats already in the plugin I'd love to accept your Pullrequest!

Tip:

From my tests, using MessagePack in itself is slower than JSON when it comes to serialization, but µWs has an easier time transmitting it than a JSON string, thus netting a performance improvement. Might need more testing.

Methods / Variables

Primus.rooms

Turborooms' Object used to maintain rooms and their clients. Structure:

{Roomname: [Spark_id1, Spark_id2, ...]}

Spark.join(roomname)

Add the spark to the passed roomname

Spark.leave(roomname)

Remove the spark from the passed roomname

Primus.batchWrite(rooms, data)

Write the passed data to all the clients in the passed room(s), similar to how Primus.write works

Primus.batchEmit(rooms, message, ...data)

Wrapper calling .batchWrite for a hacky implementation / replication of the Primus-emit .emit style message emitting.

License

MIT