Fork of herald-client that use rabbitmq as server to sending messages
npm install rabbitmq-herald-clientrabbitmq-herald-client
===========================
Fork of herald-client. It is not realy work =).
Install with:
npm install rabbitmq-herald-client
Dependencies:
bramqp-wrapper
Simple example:
``js
var hc = new (require('rabbitmq-herald-client'))();
hc.on('error', function(error){
console.log('HC error:', error);
});
setTimeout(function() {
hc.subscribe("channel1", function (sendedBy, obj) {
console.error('chan1:', obj);
});
hc.subscribe("channel2", function (sendedBy, obj) {
console.log('chan2:', sendedBy, obj);
});
setTimeout(function () {
hc.unsubscribe("channel1", function(err){
if (err) console.error('Error in unsubscribing', err);
});
setInterval(function(){
hc.publish('channel1', 'C1: '+Math.random());
hc.publish('channel2', 'C2: '+Math.random());
},10);
}, 1000);
}, 1000);
process.on('SIGINT', function(){
hc.close();
process.exit();
});
process.on('SIGTERM', function(){
hc.close();
process.exit();
});
`
In this example hc will try connect to 127.0.0.1:8765.
options is an Object. May be {}. Contains properties:.logger
* - Logger object - to log inner events.name
- String|Numeric - your application class identified. Default: Math.floor(Math.random()1000000).uid
* - String|Numeric - your application unique identified. name + '_' + Math.floor(Math.random()*1000000)
Default: .messageMaker
* - Object. Some module, that make and parse messages. See below. Default: crypt-maker.connect
* - Object. Options for new.Socket.connect. Default: {port: 5672, user: 'guest', password:'guest'} {port:5672}
See [https://nodejs.org/api/net.html#net_net_connect_options_connectionlistener]
(https://nodejs.org/api/net.html#net_net_connect_options_connectionlistener). Default:
If use crypt-maker and if algorithm && algorithm !== 'no' and no key passed to constructor - throws error.
Connect to server. If options aren't passed, connect with last options.
If already connected - do nothing.
Options are:
* options {Object} [optional]
Supports the following properties:
* port {Number} [optional]host
* {String} [optional]backlog
* {Number} [optional]vhost
* {String} [optional] AMQP parameter. default '/'user
* {String} [optional] AMQP parameter. default 'guest'password
* {String} [optional] AMQP parameter. default 'guest'path
* {String} [optional]exclusive
* {Boolean} [optional]
For all info about this see: See
[https://nodejs.org/api/net.html#net_net_connect_options_connectionlistener]
(https://nodejs.org/api/net.html#net_net_connect_options_connectionlistener).
Stops the client and close connect from accepting new connections and keeps existing
connections.
* callback {Function} [optional]
Create subscribing on server. Then event emitted - call callback(senderIdentifier, bodyOfMessage). hc
If error on subscribe will emit 'error' event.
* eventName {String} - name of eventcallback
* {Function}
Unsubscribe from event with name. If no callback and error on unsubscribe hc will emit 'error' event.
* eventName {String} - name of eventcallback
* {Function} [optional]
Publish event on server. If no callback and error on publish hc will emit 'error' event.
* whom {String} - application name to send.eventName
* {String} - name of eventeventBody
* {String | Object} - body of eventcallback
* {Function} [optional]
Send event message to one application with name.
(Will send to one instance, but not for all application with name).
* whom {String} - application name to send.eventName
* {String} - name of eventeventBody
* {String | Object} - body of eventcallback
* {Function}
Send event message to one application with uid.
* whomUid {String} - application name to send.eventName
* {String} - name of eventeventBody
* {String | Object} - body of eventcallback
* {Function}
Try call procedure on application with name.
(Will send to one instance, but not for all application with name).
* whom {String} - application name to send.action
* {Object} - action object, that you want to call at application.action.name
* {String} - action name, that you want to call at application.action.args
* {Object | String | Number | Array} - arguments for action. !!action.args should return true.options
{Object} [optional] Options for rpc. not ready yet*.callback
* {Function} [optional]
Try call procedure on application with uid.
* whomUid {String} - application name to send.action
* {Object} - action object, that you want to call at application.action.name
* {String} - action name, that you want to call at application.action.args
* {Object | String | Number | Array} - arguments for action. !!action.args should return true.options
{Object} [optional] Options for rpc. not ready yet*.callback
* {Function} [optional]
Send some message to server. At now ignored by server
* header {Object} - header of message to send.body
* {Object|string} - body of message yo send.options
{Object} [optional] Options for rpc. not ready yet*.callback
* {Function} [optional]
Add function to work with rpc calls.
* actionName {String} - action name.callback
* {Function} - function to call. See
doc
Returns true if added or false if there was function with such name.
Remove function from work with rpc calls.
* actionName {String} - action name.
Returns true if was such function else returns false.
Calling unref on a client will allow the program to exit if this is the onlyunref
active client in the event system. If the server is already d callingunref again will have no effect..connect()
Every success create new connection. That's why you should call unref again.
Opposite of unref, calling ref on a previously unrefd server will notref
let the program exit if it's the only server left (the default behavior). If
the server is d calling ref again will have no effect.
Emitted when a server send 'ping' event.
Emitted when the socket has been connected to server.
Emitted when the socket closes. Note that if connections exist, this
event is not emitted until all connections are ended.
* {Error Object}
Emitted when an error occurs. The 'close' event will be called directly
following this event.
Every message should has header and body.header
If there is no or body - message will not sent.
Be careful by default without any encrypt algorithm any can connect to your server if he know format.
Example of message to authorize (without encrypt):
``
'{"rpc":"herald-server","action":"authorize","actionId":7,"name":"156512",
"uid":"156512_86835","messId":76688,"retry":5,"timeout":15000}\r\n
{"args":{"wellKnown":"pHrAsE","name":"156512","uid":"156512_86835","rand":459}}\r\n\r\n'
If there is some connection with same uid - will not authorize new connection and close it.
Fields:
* messId Number - id of message. Client set it automaticaly.name
* String - connection name. Used for whispering and rpc. Client set it to HeraldClient.name.uid
* String - connection uid. Used for whispering and rpc. Unique for every connect. Client set it to HeraldClient.uid.retry
* Number [optional] - Count of retries of sending this message. If no field - will not retry.timeout
Now it is ignored by server. Will work soon.
* Number [optional] - Duration in ms to wait answer from client. If no field - will not retry.
Now it is ignored by server. Will work soon.
Event:
* whisp String [optional] - name of connection to send event message.whispUid
* String [optional] - uid of connection to send event message.event
* String - event name. If no whisp or whispUid sends to all subscribers.
RPC:
* actionId Number - id of action. Client set it automaticaly.action
* String - name of action.rpc
* String [optional] - name of connection to send rpc message.rpcUid
* String [optional] - uid of connection to send rpc message.rpcRegExp
* String|RegExp [optional] - regexp to find connections by name to send rpc message.
Body can by plain string, json, number or something else, except functions.
Examples shown without any encryption.
RPC by client name message:
`js`
'{"rpc":"applicationToCall","action":"actionToCall","actionId":numberActionIdFromSender,
"name":"nameOfSender","uid":"uidOfSender","messId":numberMessageId}\r\n
{"args":{argsObject}}\r\n\r\n'
RPC by client UID message:
`js`
'{"rpcUid":"applicationToCall","action":"actionToCall","actionId":numberActionIdFromSender,
"name":"nameOfSender","uid":"uidOfSender","messId":numberMessageId}\r\n
{"args":{argsObject}}\r\n\r\n'
Whispering message:
`js`
'{"whisp":"nameOfAppToWhisp","event":"someSecretEvent","name":"nameOfSender","uid":"uidOfSender",
"messId":numberMessageId}\r\n"eventBody"\r\n\r\n'
Event message:
`js``
'{"event":"someEvent","name":"nameOfSender","uid":"uidOfSender","messId":numberMessageId}\r\n"eventBody"\r\n\r\n'
Copyright (c) 2015 Konstantine Petryaev
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.