src/wampy.js
/**
     * Subscribe to a topic on a broker
     *
     * @param {string} topicURI
     * @param {function|object} callbacks - if it is a function - it will be treated as published event callback
     *                          or it can be hash table of callbacks:
     *                          { onSuccess: will be called when subscribe would be confirmed
     *                            onError: will be called if subscribe would be aborted
     *                            onEvent: will be called on receiving published event }
     * @param {object} advancedOptions - optional parameter. Must include any or all of the options:
     *                          { match: string matching policy ("prefix"|"wildcard") }
     *
     * @returns {Wampy}
     */
Subscribe to a topic on a broker
@param {string} topicURI
@param {function|object} callbacks - if it is a function - it will be treated as published event callback
or it can be hash table of callbacks:
{ onSuccess: will be called when subscribe would be confirmed
onError: will be called if subscribe would be aborted
onEvent: will be called on receiving published event }
@param {object} advancedOptions - optional parameter. Must include any or all of the options:
{ match: string matching policy ("prefix"|"wildcard") }

@returns {Wampy}
Subscribe to a topic on a broker @param { string } topicURI @param { function|object } callbacks - if it is a function - it will be treated as published event callback or it can be hash table of callbacks : { onSuccess : will be called when subscribe would be confirmed onError : will be called if subscribe would be aborted onEvent : will be called on receiving published event } @param { object } advancedOptions - optional parameter . Must include any or all of the options : { match : string matching policy ( " prefix " | " wildcard " ) } @returns { Wampy }
subscribe (topicURI, callbacks, advancedOptions) {
        let reqId, patternBased = false;
        const options = {};
        if ((typeof (advancedOptions) !== 'undefined') &&
            this._isPlainObject(advancedOptions) &&
            Object.prototype.hasOwnProperty.call(advancedOptions, 'match')) {
            if (/prefix|wildcard/.test(advancedOptions.match)) {
                options.match = advancedOptions.match;
                patternBased = true;
            }
        }
        if (!this._preReqChecks({ topic: topicURI, patternBased: patternBased, allowWAMP: true },
            'broker',
            callbacks)) {
            return this;
        }
        if (typeof callbacks === 'function') {
            callbacks = { onEvent: callbacks };
        } else if (!this._isPlainObject(callbacks) || typeof (callbacks.onEvent) === 'undefined') {
            this._cache.opStatus = WAMP_ERROR_MSG.NO_CALLBACK_SPEC;
            if (this._isPlainObject(callbacks) && callbacks.onError) {
                callbacks.onError({ error: this._cache.opStatus.description });
            }
            return this;
        }
        if (!this._subscriptions[topicURI] || !this._subscriptions[topicURI].callbacks.length) {
            reqId = this._getReqId();
            this._requests[reqId] = {
                topic: topicURI,
                callbacks,
                advancedOptions
            };
            this._send([WAMP_MSG_SPEC.SUBSCRIBE, reqId, options, topicURI]);
        } else {    
            if (this._subscriptions[topicURI].callbacks.indexOf(callbacks.onEvent) < 0) {
                this._subscriptions[topicURI].callbacks.push(callbacks.onEvent);
            }
            if (callbacks.onSuccess) {
                callbacks.onSuccess({
                    topic: topicURI,
                    subscriptionId: this._subscriptions[topicURI].id
                });
            }
        }
        this._cache.opStatus = WAMP_ERROR_MSG.SUCCESS;
        this._cache.opStatus.reqId = reqId;
        return this;
    }
subscribe ( topicURI , callbacks , advancedOptions ) { let reqId , patternBased = false ; const options = { } ; if ( ( typeof ( advancedOptions ) !== 'undefined' ) && this . _isPlainObject ( advancedOptions ) && Object . prototype . hasOwnProperty . call ( advancedOptions , 'match' ) ) { if ( / prefix|wildcard / . test ( advancedOptions . match ) ) { options . match = advancedOptions . match ; patternBased = true ; } } if ( ! this . _preReqChecks ( { topic : topicURI , patternBased : patternBased , allowWAMP : true } , 'broker' , callbacks ) ) { return this ; } if ( typeof callbacks === 'function' ) { callbacks = { onEvent : callbacks } ; } else if ( ! this . _isPlainObject ( callbacks ) || typeof ( callbacks . onEvent ) === 'undefined' ) { this . _cache . opStatus = WAMP_ERROR_MSG . NO_CALLBACK_SPEC ; if ( this . _isPlainObject ( callbacks ) && callbacks . onError ) { callbacks . onError ( { error : this . _cache . opStatus . description } ) ; } return this ; } if ( ! this . _subscriptions [ topicURI ] || ! this . _subscriptions [ topicURI ] . callbacks . length ) { reqId = this . _getReqId ( ) ; this . _requests [ reqId ] = { topic : topicURI , callbacks , advancedOptions } ; this . _send ( [ WAMP_MSG_SPEC . SUBSCRIBE , reqId , options , topicURI ] ) ; } else { if ( this . _subscriptions [ topicURI ] . callbacks . indexOf ( callbacks . onEvent ) < 0 ) { this . _subscriptions [ topicURI ] . callbacks . push ( callbacks . onEvent ) ; } if ( callbacks . onSuccess ) { callbacks . onSuccess ( { topic : topicURI , subscriptionId : this . _subscriptions [ topicURI ] . id } ) ; } } this . _cache . opStatus = WAMP_ERROR_MSG . SUCCESS ; this . _cache . opStatus . reqId = reqId ; return this ; }
Subscribe to a topic on a broker
 @param {string} topicURI
 @param {function|object} callbacks - if it is a function - it will be treated as published event callback
                          or it can be hash table of callbacks:
                          { onSuccess: will be called when subscribe would be confirmed
                            onError: will be called if subscribe would be aborted
                            onEvent: will be called on receiving published event }
 @param {object} advancedOptions - optional parameter.
Subscribe to a topic on a broker @param { string } topicURI @param { function|object } callbacks - if it is a function - it will be treated as published event callback or it can be hash table of callbacks : { onSuccess : will be called when subscribe would be confirmed onError : will be called if subscribe would be aborted onEvent : will be called on receiving published event } @param { object } advancedOptions - optional parameter .
// no such subscription or processing unsubscribing // WAMP SPEC: [SUBSCRIBE, Request|id, Options|dict, Topic|uri] // already have subscription to this topic // There is no such callback yet