Giter Club home page Giter Club logo

wamp.io's Introduction

WAMP.IO: Autobahn WebSockets RPC/PubSub

This is an implentation of the WebSocket Application Messaging Protocol (WAMP) proposed by Travendo.

It attaches to a WebSocket server and provides mechanisms for

  • Request and Response and
  • Publish and Subscribe

Usage

Simple PubSub server

By default, wamp.io provides a simple PubSub server that enables client to subscribe to any topic and to send events to any topic.

Attach WAMP to a WebSocket.IO server

var wsio = require('websocket.io')
  , wamp = require('wamp.io');

var ws = wsio.listen(9000);
var app = wamp.attach(ws);

Attach WAMP to an Engine.IO server

var eio = require('engine.io')
  , wamp = require('wamp.io');

var engine = eio.listen(9000);
var app = wamp.attach(engine);

Simple RPC server

The server emits the call event when an RPC function is called. Results can be returned using the callback parameter.

var wsio = require('websocket.io')
  , wamp = require('wamp.io');

var ws = wsio.listen(9000);
var app = wamp.attach(ws);

app.on('call', function(procUri, args, cb) {
  if (procUri === 'isEven') {
    cb(null, args[0] % 2 == 0);
  }
});

Simple RPC client

var when = require('when')
  , wamp = require('wamp.io');
  
var app = wamp.connect('ws://localhost:9000',
    // WAMP session was established
    function (session) 
    {
      console.log('new wamp session');
      
      session.call("test:isEven", 2)      
        .promise.then(
            // RPC success callback
            function (reply)
            {
              console.log("result: " + reply);
            },

            // RPC error callback
            function (error, desc) 
            {        
              console.log("error: " + desc);
            }
        );      
    },

    // WAMP session is gone
    function (session) 
    {
      console.log('wamp session is gone');
    }
  );  

License

(The MIT License)

Copyright (c) 2013 Nico Kaiser <[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.

wamp.io's People

Contributors

lamuertepeluda avatar lrascao avatar nicokaiser avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wamp.io's Issues

npm install

nom install does not work
It seems package.json has some problem.

Valid install targets:
npm ERR! ["0.1.1","0.1.2","0.1.4","0.1.5","0.1.6","0.2.0","0.2.1"]

RPC call async problem

It seems to me that there is a bug in the RPC call/result sync. The problem occurs when two (or more) calls are made to the library on very short interval and the server code does something asynchronously, then the returning callbacks are all returning a value to the last call send to Wamp.io.

Here is an example code with my added debug prints.

handlers[protocol.TYPE_ID_CALL] = function(client, args) {
  callId = args.shift();
  console.log("Call id: "+ callId);
  prefixes.resolveOrPass(procUri = args.shift());
  args = args || [];

  // Callback function
  cb = function(err, result) {
    var msg;
    console.log("Result id: "+ callId);
    if (err) {
      msg = [protocol.TYPE_ID_CALL_ERROR, callId, 'http://autobahn.tavendo.de/error#generic', err.toString()];
    } else {
      msg = [protocol.TYPE_ID_CALL_RESULT, callId, result];
    }
    client.send(JSON.stringify(msg));
  };

  this.emit('call', procUri, args, cb);
};

My client code sends two rpc calls and here are the debug prints.
Call id: 0.e1elbygmbg6fajor
Call id: 0.86y08973bkervn29
Result id: 0.86y08973bkervn29
Result id: 0.86y08973bkervn29

So the problem to me seems to be that the scope of the callId variable is local and that is changed whenever a new call is done. I will investigate more to find out a possible solution..

How to handle client conection problems?

Trying to connect client.js example to a non running server do a Node.js crash. How can I handle connection problems? WebSocket object has an "On Error" event but can't see how to handle using publics Client or Session objects. By now my solutions is to hack Client code and add

 ws.on('error', function(error) {...})

to constructor. Is there a cleaner solution?

Thanks.

lib/server.js problem

When (in examples/pubsub/server.js)

51    app.publish(topicUri, event);

is being called, it seems the following codes could not be initialised,

(lib/server.js)

function Server(options) {
  this.options = options || {};
  this.topics = {};
  this.clients = {};
}

because, at line 108
(lib/server.js)

if (this.topics[topicUri]) 

where this is still empty:
Screen Shot 2013-03-04 at 12 19 27

Thus, the if condition is always false.

Is there any instruction how to use server.js in lib/server.js?

pub/sub?

The docs say it supports pub/sub, but doesn't give any examples of how to do it... How would the server publish or subscribe?

Something wrong in description

Probably you would not use WebSocket.IO in your code? and it has switched to WS?

and by the way, there are some things that confused me:

  1. in lib/server.js
115 this.clients[id].send(msg);

where is the send from? my IDE shows me from express or jQuery, but you haven't used any of those
2. how do use the debug in your code?

Screen Shot 2013-03-14 at 10 26 02

How to manually subscribe a client to an event?

I've a use case where after a CALL server know that this client want to SUBSCRIBE to a event channel, so is unnecessary that client send a SUBSCRIBE msg. How can I resolve that?
Many thanks.

A Client Quits Will Cause The Server Down

When it happen:

I opened two clients in different windows on Safari. One sent a message and the other could received, but if either of the clients quit, such as by close the window, it would trigger an error on the server.

What is the output:

The Node.js output says:

wamp.io/lib/server.js:90
      delete self.topics[topic][client.id];
                                      ^
TypeError: Cannot convert null to object
    at WebSocket.Server.onConnection (/Users/Ken/Documents/Projects/wamp.io/lib/server.js:90:39)
    at WebSocket.EventEmitter.emit (events.js:123:20)
    at WebSocket.cleanupWebsocketResources (/Users/Ken/Documents/Projects/wamp.io/node_modules/ws/lib/WebSocket.js:649:23)
    at Socket.EventEmitter.emit (events.js:90:17)
    at TCP.onread (net.js:417:51)

What I thought:

If added one line before the delete self.topics[topic][client.id];

console.log(self.topics[0]);

it outputs undefined, moreover.

the self.topics looks like an object rather than an array, e.g.:

{ 'event:firstevent': { '585972943854826': true } }

Make release on npm, add install instructions

Right now this is required to install the package:

{
    "dependencies": {
        "wamp.io": "git+https://github.com/nicokaiser/wamp.io"
    }
}

This should probably be documented in the README, or you could just release a package in the npm registry.

It seems a bit different from WAMP specification

I am not sure if I understand correctly, it seems your server only accept PUBLISH and the format is a bit different from WAMP specification? http://wamp.ws/spec#publish_message

It says: publish (a) message with 'string' as payload [7, "http://example.com/simple", "Hello, world!"]

While your code:
try {
var parsed = qs.parse(buffer);
console.log(parsed);
topicUri = parsed.topicuri;
event = JSON.parse(parsed.body);
}

It seems you need "topicuri" and "body", but the server does not accept: {"topicuri":"http://example.com/simple","body":"something"} or topicuri=http://example.com/simple&body=something
(based on https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse, it should accept body=something)

While it will accept: topicuri=http://example.com/simple&body={"event":"helleo"}
(and this way is different from the specification, maybe?)

Screen Shot 2013-03-01 at 16 27 03

New WebSocket Object does not return WAMP subprotocol?

As the client side library, Autobahn.js have problems on connecting your example server. Thus, I tried the following comparison,
The first one will connect to Autobahn.js testing WAMP server, by running command:
more here: http://autobahn.ws/js/getstarted

wstest -d -m wampserver -w ws://localhost:9000

Then, in Chrome console, I run:

var ws = new WebSocket("ws://localhost:9000",["wamp"])

then the ws will be initialised:

WebSocket {binaryType: "blob", extensions: "", protocol: "wamp", onclose: null, onerror: null…}

_As you can see that the protocol contains "wamp"_

So I did the same thing on your example server (wamp.io/examples /pubsub/server.js)
either
corresponding to

15 var ws = wsio.listen(9000);
var ws = new WebSocket("ws://localhost:9000",["wamp"])

or
corresponding to

61 api.listen(9090);
var ws = new WebSocket("ws://localhost:9090",["wamp"])

but the different is the ws giving no protocol:

WebSocket {binaryType: "blob", extensions: "", protocol: "", onclose: null, onerror: null…}

I also tried mine on top of express, which I asked you days ago,

...
var app = express();
var server = http.createServer(app);
var wamp = wampio.attach(wsio.attach(server));
app.configure(function(){
    app.set('port', process.env.PORT || 3000);
    ...
}
...
server.listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});
var ws_1 = new WebSocket("ws://localhost:3000",["wamp"])

but it returns the same ws without protocol.

I think this might be the reason that AutoBahn.js cannot contact to. What do you think?
Screen Shot 2013-03-05 at 12 53 54
(In the image, I changed your port to 8000 rather than 9000)

SockJS support?

Is there any support planned for https://github.com/sockjs/sockjs-node?

I believe (perhaps you can correct me on this) the only abstraction that is missing is the name of the command to send on the socket (currently .send, SockJS uses .write) and the event name for incoming data (currently "message", SockJS uses "data"). I made the change to test it and it works correctly (validated using AutobahnJS through SockJS-client on the client side), but perhaps there are some internals I'm not aware about that could break in some cases.

sockjs/sockjs-node#67 is a related discussion where it was decided SockJS would not rename the call to .send and the event name to "message".

ws as dev dependency?

I was wondering, why is ws currently a dev dependency (and not a dependency altogether) in the package.json? I cannot use the wamp.io module if it doesn't have ws installed.

Thank you.

RPC message arguments

It seems there is something not work with the WAMP specification, in lib/handlers.js (but I am not sure whether my understanding is correct or not)

38 handlers[protocol.TYPE_ID_CALL] = function(client, args) {
39  callId = args.shift();
40  prefixes.resolveOrPass(procUri = args.shift());
41  args = args || [];
  ...
55  this.emit('call', procUri, args, cb);
56};

After line 41, the args will turn to an array like Object, meaning the args looks like an array, but the type of it is an Object.
e.g. the RPC message argument in [2,"4qK7nmQUsumUSGUM","calc:sum",[1,2,3,4,5]] is [1,2,3,4,5], but after your code line 41, it then changed to [ [ 1, 2, 3, 4, 5 ] ]

For more information, please check this screen shot.
Screen Shot 2013-03-17 at 15 29 22

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.