Primus Resource
Define resources with auto-binded methods that can be called remotely on top of Primus. This plugin depends on primus-multiplex
and primus-emitter
however if you disable multiplexing then you can omit installing primus-multiplex
.
Method on an object prototype in the form of on
+ method, like onupdate
will be automatically binded as an event
on all incoming sparks
, then the event can be called remotely by the client by just invoking the method name without the on
like update
.
Compatibility
This plugin works with any Primus version, just make sure the primus-emitter
or primus-multiplex
are compatible with the primus version you are using.
Instalation
$ npm install primus-resource
primus-resource
depends on primus-emitter
and primus-multiplex
plugins so you will need to install those plugins:
$ npm install primus-multiplex primus-emitter
Usage
On the Server
Initializing server like this:
var resource = Primus = http = ; var server = http; // Primus servervar primus = server; // Add dependencies and use resource pluginprimus;
Or with primus.io
:
var resource = Primus = http = ; var server = http; // Primus servervar primus = server; // Use resource pluginprimus;
Then create a resource like this:
// Defining a resource {} Creatureprototype { console; ;}; Creatureprototype { // make the creature walk with some code console; ;}; // Initialize our resourceprimus; server;
You can also create a resource like this:
// Create our resourcevar Creature = primus; Creature { console; ;}; Creature { // make the creature walk with some code console; ;}; server;
Or like this by passing the object directly to the resource method:
// Create our resourcevar Creature = { console; ; } { // make the creature walk with some code console; ; } ; // Initialize resourceprimus; server;
On the Client
var primus = Primus; // connect to resourcevar creature = primus; // wait until resource is readycreature;
Disabling multiplex
You can always disable multiplexing by passing a false
as the last parameter on the server and on the client, this is required on both sides. If you disable multiplexing you can omit installing primus-multiplex
.
On the server:
primus;
On the client:
primus;
Some conventions
- The remote method naming convention is
on
+name example:blog.oncreate
. - Names should be lowercase so use
blog.onupdate
instead ofblog.onUpdate
. - Call method on the client side without the
on
so for calling the previous method doblog.update(data, fn)
.
Run tests
$ make test
Todo
- Allow remote methods not only on prototype. I am using the prototype just to fit my needs but will extend soon.
- Make a better API.
Other plugins
License
(The MIT License)
Copyright (c) 2013 Jonathan Brumley <cayasso@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.