Backbone.WAMP
Backbone.WAMP replace your classic REST protocol, based on AJAX, to modern WAMP protocol, based on WebSockets. You can read more about here.
Dependencies
Backbone
AutobahnJS
Overview example
/************ browser************/ windowWAMP_OTHER_ID = "nodejs";windowWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; WAMP_CONNECTION { var Collection = WampCollection; var collection = ; collection; // call wampRead below collection;}; WAMP_CONNECTION; /********** nodejs**********/ globalWAMP_MY_ID = "nodejs";globalWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; WAMP_CONNECTION { var Collection = WampCollection; collection = ;}; WAMP_CONNECTION;
API
wampConnection / WAMP_CONNECTION
Before create instances of WampModel
/ WampCollection
,
you need eastablish autobahn connection and link it to global WAMP_CONNECTION
or
specific wampConnection
for WampModel
/ WampCollection
.
windowWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; var wampConnection = url: "ws://127.0.0.1:9000/ws" realm: "realm2"; WAMP_CONNECTION { var Collection = WampCollection; var collection = ; // this collection used WAMP_CONNECTION}; wampConnection { var Collection = WampCollection; var collection = ; // this collection used wampConnection} WAMP_CONNECTION;wampConnection;
wampMyId / WAMP_MY_ID
This unique peer-id of current environment, with which you want interact from other environments.
globalWAMP_MY_ID = "nodejs";globalWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; WAMP_CONNECTION { var Collection = WampCollection; var Model = WampModel; var collection = ; // this available on "nodejs" var model = ; // this available on "nodejs2"}; WAMP_CONNECTION;
wampOtherId / WAMP_OTHER_ID
This means peer-id of environment, with which you want interact from current environment.
windowWAMP_OTHER_ID = "nodejs";windowWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; WAMP_CONNECTION { var Collection = WampCollection; var Model = WampModel; var collection = ; // this will interact with "nodejs" var model = ; // this will interact with "nodejs2"}; WAMP_CONNECTION;
constructor
When you create instances of WampModel
/ WampCollection
, this automatically
register listeners, using wampMyId
/ WAMP_MY_ID
and urlRoot
/ url
, if it present.
For ignoring this, need pass to constructor
- wampNoAttach
option.
For catching each registering need pass to constructor
- wampRegister
callback-option.
windowWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; WAMP_CONNECTION { var Collection = WampCollection; var Model = WampModel; var Model2 = WampModel var collection = ; // no register listeners, wampMyId / WAMP_MY_ID ommited var model = ; // no register listeners, urlRoot ommited var model2 = {} wampNoAttach: true; // no register listeners, pass specific option model2 = {} { // register listeners // calling for registering each action: create | read | update | delete | patch }}; WAMP_CONNECTION;
wampCreate, wampRead, wampUpdate, wampDelete, wampPatch
This hooks automatically registers, when create instances of WampModel
/ WampCollection
,
if exists wampMyId
/ WAMP_MY_ID
and urlRoot
/ url
and not pass wampNoAttach
option.
sendOptions
Received param: sendOptions.data
contains JSON-parsed model for create, update, patch actions.
Or GET-params for read action of model/collection.
sendOptions.extra
contain any extra information, which you can send in options.wampExtra
Built-in options in sendOptions.extra
:
wampModelId
of specific model, if neededwampMyId
of another environment
Return
Need return data or promise, that resolved to data.
For error need return new autobahn.Error(...)
or promise, that resolve new autobahn.Error(...)
For example, see Overview example above.
wampGetUri / WAMP_GET_URI
By default, via methods save
, fetch
, e.t.c. generated WebSocket messages as is peerId.uri.action
Example: "nodejs.test_collection.create"
You can overwrite template if needed via wampGetUri
/ WAMP_GET_URI
, that
Received params: peerId, uri, action
Return: string of WebSocket message
wampAuth / WAMP_AUTH
When defined, will be called before wampCreate, wampRead, wampUpdate, wampDelete, wampPatch.
If it's return non true value or promise, that resolved to non true,
wampCreate, wampRead, wampUpdate, wampDelete, wampPatch not be called.
Received params: uriOptions, sendOptions
uriOptions
contain peerId, uri, action
more about sendOptions
in wampCreate, wampRead, ... partition
Return: true or promise, that resolved to true (if auth passed)
globalWAMP_MY_ID = "nodejs";globalWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; WAMP_CONNECTION { var Collection = WampCollection; var collection = ;}; WAMP_CONNECTION;
Session
This can be useful for wampAuth
.
For session mechanism need dynamically generate wampMyId
.
Example: browser_0dbee552-46e0-4c7c-aa4e-e1341dc00b18
Use any UUID, GUID, e.t.c. generator, maybe node-uuid
wampUnregister
Call this method directly on instances of WampModel
/ WampCollection
for unregistering
wampCreate, wampRead, ... methods.
Received params: actions, callback
actions
it array, for example ["read", "delete"]
If omitted, will unregister all actions
callback
called per each unregistering action
globalWAMP_MY_ID = "nodejs";globalWAMP_CONNECTION = url: "ws://127.0.0.1:9000/ws" realm: "realm1"; WAMP_CONNECTION { var Collection = WampCollection; var collection = ; collection;}; WAMP_CONNECTION;
License
(The MIT License)
Copyright (c) 2015 Vladislav Botvin <darkvlados@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.
License of AutobahnJS
Copyright (c) 2011-2014 Tavendo GmbH.
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.