The 720kb notifier api (atm it uses websockets)
npm install comunicatorIt uses a well defined protocol in order to simplify the integration with other back ends (even if in different languages).
The client could be used in AngularJS, via provider, or in plain javascript.
Comunicator is developed by 720kb.
This implementation needs a browser that is capable to instantiate websockets, due to the fact that it wraps a websocket.
The comunicator is an Observable and uses the RxJs implementation.
The AngularJS provider needs at least AngularJS version 1.2.
Comunicator could be installed via npm or bower:
#### NPM
``sh`
$ npm install --save comunicator`
#### Bowersh`
$ bower install --save comunicator
javascript
const jwtSalt = '_super_secret_salt_12345'
, Comunicator = require('comunicator').Comunicator
, theComunicator = new Comunicator({
'host': '::',
'port': 3000
}, jwtSalt);
`The websocket configuration object refers to the options passed to ws.Server constructor.
The methods exposed are:
-
broadcast(whoami, what): this sends what to everyone who is connected. The message is sent to the others with whoami as sender;-
sendTo(whoami, who, what): this sends what to who. The message is sent to who with whoami as sender;-
isUserPresent(who): checks if who is connected to Comunicator.A subscriber to the comunicator could receive:
`javascript
{
'type': 'ready'
}
`
When the underlined websocket is instantiated;`javascript
{
'type': 'open',
'whoami':
}
`
When a user is conneced to the server;`javascript
{
'type': 'user-leave',
'whoami':
}
`
When a user closes the Comunicator connection;`javascript
{
'type': 'user-joined',
'whoami':
}
`
When a user fulfills the connection in Comunicator;`javascript
{
'type': 'no-pending-messages',
'whoami':
}
`
When there aren't messages for the identified user;`javascript
{
'type': 'message-arrived',
'whoami': ,
'who': ,
'what':
}
`
When a message is arrived.In _broadcast_-ed messages the recipient is
* due to the fact that the message is for everyone;$3
The files you need are:
- dist/frontend/comunicator-min.js for the plain javascript implementation;
- dist/frontend/angular-comunicator-min.js for the AngularJS implementation.There are also the not minified versions available in the same folder (without
min).If you are about to use the AngularJS provider you have to include the module that brings the provider, for example:
`js
angular.module('app', [
'720kb.comunicator'
]);
`#### Plain javascript
Comunicator client is an UMD module (it can be used also in node.js programs). After you load the comunicator file with what you prefer (SystemJS, RequireJs, Browserify, ...) to use it you need to create an instance of comunicator, for example:
`javascript
const comunicator = new Comunicator();
`
or
`javascript
const comunicator = new Comunicator();
`The comunicator client object expose this methods:
-
userIsPresent(whoami, token): this sends to comunicator server who the client is and which jwt token will be used;-
broadcast(what, managed): this sends what to everyone who is connected;-
sendTo(who, what, managed): this sends what to who.and also have the
whoAmI property that returns the client identifier stored in comunicator client.As you can see
sendTo and broadcast methods have the managed parameter. Comunicator server forwards messages from client to client by default. If a client doesn't want this behaviour, should call the method with a truthy value to managed parameter.A subscriber to the comunicator client could receive:
`javascript
{
'type': 'open',
'whoami':
}
`
When the underlined comunicator websocket is opened;`javascript
{
'type': 'joined',
'whoami':
}
`
When the comunicator client has finished the joining process;`javascript
{
'type': 'to-me',
'whoami': ,
'who': ,
'what':
}
`
When the comunicator client receives a directly sent message.`javascript
{
'type': 'to-all',
'whoami': ,
'what':
}
`
When the comunicator client receives a broadcasted message.#### AngularJS
The provider exposes the
setComunicatorServerURL( that must be called to instantiate and configure the Comunicator object.Done that, the Comunicator object is available via the provider. Please note that you need to run
$scope.$apply()` method to update your view after a reaction from comunicator.We will be very grateful if you help us making this project grow up.
Feel free to contribute by forking, opening issues, pull requests etc.
The MIT License (MIT)
Copyright (c) 2014 Dario Andrei, Filippo Oretti
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.