Giter Club home page Giter Club logo

primus-resource's Introduction

Primus Resource

Build Status NPM version

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 = require('primus-resource')
  , Primus = require('primus')
  , http = require('http');

var server = http.createServer();

// Primus server
var primus = new Primus(server);

// Add dependencies and use resource plugin
primus
.use('multiplex', 'primus-multiplex')
.use('emitter', 'primus-emitter')
.use('resource', resource);

Or with primus.io:

var resource = require('primus-resource')
  , Primus = require('primus.io')
  , http = require('http');

var server = http.createServer();

// Primus server
var primus = new Primus(server);

// Use resource plugin
primus.use('resource', resource);

Then create a resource like this:

// Defining a resource
function Creature() {}

Creature.prototype.oncommand = function (spark, command, fn) {
  console.log(command);
  fn('Creature just got command: ' + command);
};

Creature.prototype.onwalk = function (spark, fn) {
  // make the creature walk with some code
  console.log('walk');
  fn('Creature started to walk');
};

// Initialize our resource
primus.resource('creature', new Creature());

server.listen(8080);

You can also create a resource like this:

// Create our resource
var Creature = primus.resource('creature');

Creature.oncommand = function (spark, command, fn) {
  console.log(command);
  fn('Creature just got command: ' + command);
};

Creature.onwalk = function (spark, fn) {
  // make the creature walk with some code
  console.log('walk');
  fn('Creature started to walk');
};

server.listen(8080);

Or like this by passing the object directly to the resource method:

// Create our resource
var Creature = {
  
  oncommand: function (spark, command, fn) {
    console.log(command);
    fn('Creature just got command: ' + command);
  },

  onwalk: function (spark, fn) {
    // make the creature walk with some code
    console.log('walk');
    fn('Creature started to walk');
  }

};

// Initialize resource
primus.resource('creature', Creature);

server.listen(8080);

On the Client

var primus = Primus.connect('ws://localhost:8080');

// connect to resource
var creature = primus.resource('creature');

// wait until resource is ready
creature.on('ready', function () {
  
  // start calling remote events
  creature.command('sleep', function (res) {
    console.log(res);
  });

  // call the server remote walk event
  creature.walk(function (res) {
    console.log(res);
  });

});

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.resource('creature', Creature, false);

On the client:

primus.resource('creature', false);

Some conventions

  • The remote method naming convention is on+name example: blog.oncreate.
  • Names should be lowercase so use blog.onupdate instead of blog.onUpdate.
  • Call method on the client side without the on so for calling the previous method do blog.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 <[email protected]>

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.

primus-resource's People

Contributors

cayasso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

primus-resource's Issues

Disconnect from resource?

I have been able to connect to a resource. But how do I disconnect from it without disconnecting the socket? For example, I enter a "creature" resource. Later I want to leave "creature" and interact with another creature.

Stream into spark?

spark is not streamable? when i try to pipe anything with primus-resource "spark" argument it does trigger data event but doesn't carry data.
Is it a bug or feature?

...
Creature.prototype.oncommand = function (spark, command, fn) {
  console.log(command); 


                var through  = require("through")
                    var RWstream = through(function write(data) {
                            this.queue(data) //data *must* not be null
                          },
                          function end () { //optional
                            this.queue(null)
                  })

 RWStream.pipe(spark);
 RWstream.write('Hello World')

  fn('Creature just got command: ' + command);
};

At client side:

var socket = Primus.connect('ws://'+window.location.host );
socket.on('open', function () {
  socket.send('hi', 'hello world');
  socket.on('hello', function (msg) {
      console.log(msg);
  });

});

socket.on("data", function(stream){
// `always getting empty string `
  console.log("Data:::::::::::::" + stream.toString()) ;
})

Possible to use ES6 class instead of object/function?

Is it possible to create a new resource using a class instead of an object or a function? I'd love to be able to use es6 class getters and setters inside my resource but I'm still learning primus and how plugins work with it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.