primus-emit module adds
server->client event emitting to
The module is released under the name
npm install --save primus-emit
--save flags tells
npm to automatically add the package and it's
installed version as dependency.
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 =server = ;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
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.Socket methods or properties as you will be compiling the client file
without the added plugins.
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
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.
To emit an event from the server to the client you can simply call the
If you want to send an event to every connected client on your server you can simply do that by iterating over the connections.
Sending events on the client is just as simple as on the server.
var primus = '';//// We can listen to events that are emitted from the server.//primus;primus;
There are a couple of differences between this module and the
module. The only similarity that they have is that they both emit events. The
main differences are:
primus-emittermodule adds a special
sendmethod to the prototypes while we re-use the
emitmethod. This makes the code much more portable as it uses the same method name node's EventEmitter.
primus-emitterships with a lot more features that are not needed for emitting events.
This module was written as part of the plugin documentation for Primus. Writing an EventEmitter was the ideal use case as it: