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-emit

v1.0.0

Published

Emit events over your primus connection.

Downloads

1,297

Readme

primus-emit

Version npmBuild StatusDependenciesCoverage StatusIRC channel

The primus-emit module adds client->server and server->client event emitting to Primus.

Installation

The module is released under the name primus-emit:

npm install --save primus-emit

The --save flags tells npm to automatically add the package and it's installed version as dependency.

Adding it to Primus

The module should be used through Primus's plugin system. In the following examples we assume that your server has been setup as:

'use strict';

var Primus = require('primus')
  , server = require('http').createServer();
  , primus = new Primus(server, { transformer: 'websockets' });

Now that the server and Primus instance has been created we can add the plugin with the server. Adding plugins is done with the primus.use method:

primus.use('emit', require('primus-emit'));

And that is everything that you need. The module doesn't require any configuration.

If you are manually saving the compiled Primus client to disk make sure you include the plugins before calling the primus.save, primus.library or primus.Socket methods or properties as you will be compiling the client file without the added plugins.

Usage

There are a couple exceptions on the events that you can emit between the server and client. We automatically blacklist reserved event names. This ensures that you cannot accidentally emit the end event on the client and close the connection on the server etc. The blacklisted events are all events that are prefixed with incomming: and outgoing: as they are used by Primus internally and all events client or server emits. See https://github.com/primus/primus#events for an overview of all events that are emitted by Primus.

Server

To emit an event from the server to the client you can simply call the emit method:

primus.on('connection', function connection(spark) {
  spark.emit('event-name', 'arguments');

  //
  // To receive events, simply add a listenern for it.
  //
  spark.on('custom-event', function custom(data, another, arg) {
    assert.equal(data.foo, 'foo');
    assert.equal(another, 1);
    assert.equal(arg, 'bar');

    this.emit('foo', 'bar');
  });
});

Broadcasting

If you want to send an event to every connected client on your server you can simply do that by iterating over the connections.

primus.forEach(function (spark) {
  spark.emit('broadcast', 'event');
});

Client

Sending events on the client is just as simple as on the server.

var primus = new Primus('http://localhost:port');

//
// We can listen to events that are emitted from the server.
//
primus.on('event-name', function (arg) {
  assert.equal(arg, 'arguments');

  //
  // Or emit our own events to the server.
  //
  this.emit('custom-event', { foo: 'foo' }, 1, 'bar');
});

primus.on('foo', function (bar) {
  assert.equal(bar, 'bar');
  primus.emit('foo', bar);
});

FAQ

How is different than primus-emitter

There are a couple of differences between this module and the primus-emitter module. The only similarity that they have is that they both emit events. The main differences are:

  1. method name The primus-emitter module adds a special send method to the prototypes while we re-use the emit method. This makes the code much more portable as it uses the same method name node's EventEmitter.
  2. Focus This module only focuses on one thing, emitting events. The primus-emitter ships with a lot more features that are not needed for emitting events.
  3. Small The footprint of this module is really small. The whole code base is only 80 lines of code including comments. We use the bare minimal code in order to work. This makes maintenance a lot easier.

Why was this module created

This module was written as part of the plugin documentation for Primus. Writing an EventEmitter was the ideal use case as it:

  • Uses the Primus message transformation for message interception.
  • Extends the client and server.
  • Is small enough to understand.

License

MIT