JsonMessagesProtocol

WebSocketProtocols~ JsonMessagesProtocol

Simple protocol for exchanging JSON commands.

Each message transferred by a web socket is a JSON with two attributes

 {command: {String}, params: {*}}

Inside a WebSocket threads you can subscribe to a messages, arrived from a clients and assign handlers to it (if you need to receive web socket messages in server):

    var wsNotifier = UB.getWSNotifier();
        if (wsNotifier) {
            console.debug('Start subscribing to wsNotifier tsts_* events');
            wsNotifier.on('tst_echo', function (connection, params) {
                connection.send({
                command: 'tst_message', params: {from: connection.session.userID, message: params}
            });
        });
    }

Inside a http threads can be used as such:

    function notifyAboutNewRecord(rowID){
        var notifier = UB.getWSNotifier();
        if (notifier) {
            //send message to ALL connected sessions
            notifier.broadcast('ub_entityModification', {action: 'insert', ID: rowID});

            //Send to specific user
            var userSessions = notifier.getUserSessions(Session.userID);
            userSessions.forEach(function(sessionID){
                notifier.sendCommand('test_command', sessionID, {action: 'inserted', ID: rowID});
            });
        }
    }

If WebSocket support are enabled in server config then instance of this protocol is accessible via UB.wsNotifier

Constructor

new JsonMessagesProtocol(namedAs)

Arguments:
  1. namedAs (String)  The name of a resulting protocol

Extends

Methods

sendCommand(command, recipient, params)Boolean

Send a specified command to recipient. Return true if data is sended (no guaranty it is recived by client)
Arguments:
  1. command (String)  Command to send
  2. recipient (Number)  User Session identifier
  3. params (*)  Any value

getUserSessions(userID) → Array:.<Number:>

Returns the IDs of all the sessions with established WebSocket connections
Arguments:
  1. userID (Number)  User identifier (from uba_user)

broadcast(command, params)

Send a specified command to all user sessions connected using this protocol
Arguments:
  1. command (String)  Command to send
  2. params (*)  Any value

setMaxListeners(n)

Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.
Arguments:
  1. n (Number)

getMaxListeners()Number

emit(type, …eventArgs)boolean

Execute each of the listeners in order with the supplied arguments. Returns true if event had listeners, false otherwise.
Arguments:
  1. type (String)  Event name
  2. ...eventArgs (*)  Arguments, passed to listeners

addListener(type, listener)EventEmitter

Adds a listener to the end of the listeners array for the specified event. Will emit newListener event on success.

Usage sample:

 Session.on('login', function () {
     console.log('someone connected!');
 });

Returns emitter, so calls can be chained.

Arguments:
  1. type (String)  Event name
  2. listener (function)

on(type, listener)EventEmitter

Alias for addListener
Arguments:
  1. type (String)  Event name
  2. listener (function)

once(type, listener)EventEmitter

Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed.
Arguments:
  1. type (String)  Event name
  2. listener (function)

removeListener(type, listener)

Remove a listener from the listener array for the specified event. Caution: changes array indices in the listener array behind the listener. Emits a 'removeListener' event if the listener was removed.
Arguments:
  1. type (String)  Event name
  2. listener (function)

removeAllListeners(type)EventEmitter

Removes all listeners, or those of the specified event. It's not a good idea to remove listeners that were added elsewhere in the code, especially when it's on an emitter that you didn't create (e.g. sockets or file streams).

Returns emitter, so calls can be chained.

Arguments:
  1. type (String)  Event name

listeners(type) → Array:.<function()>

Returns an array of listeners for the specified event.
Arguments:
  1. type (String)  Event name