Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

NodeAgentSDK Connection Best Practices

Adrian Prananda edited this page Dec 18, 2020 · 4 revisions

Below is an example of recommended best practices for using Node Agent SDK to connect and stay connected to LivePerson Messaging Service.

It is important to note that when the Agent object is instantiated it will immediately execute a multi-step connection process, this includes several http requests to generate an authentication token, the SDK then attempts to establish a websocket connection to the messaging service. The example below shows our current recommended best practices for handling any errors that occur during this process.

There are certain situations where generating an authentication token is not necessary and could potentially cause undue burden on LivePerson. The code below shows how to avoid these unnecessary calls.

Please note, this example only addresses connection concerns, please see other examples for information on sending messages, routing conversations, etc.

const Agent = require('node-agent-sdk').AgentSDK;

class StayConnectedAgent extends Agent {
    constructor(conf) {
        super(conf);

        this.conf = conf;

        this.on('connected', this.onConnected.bind(this));

        this.on('error', this.onError.bind(this));

        this.on('closed', this.onClosed.bind(this));
    }

    onConnected() {
        // TODO Make subscriptions, set online, etc
        // Refer to examples/MyCoolAgent.js for more information

        // Start the keep alive process
        // all connections to messaging service need to have at least one active request per minute
        // getClock is a quick no-op to achieve this
        this._pingClock = setInterval(this.getClock, 30000);
    }

    onError(err) {
        // Errors with codes less than 1000 indicate problems with the initial HTTP calls during authentication.
        // Since they may occur before authentication is complete, reconnect with a token generation
        if (err && err.code < 1000) {
            this.reconnectWithDelay(false);
        }
    }

    /**
     * Closed events occur when the websocket connection to the Messaging Service is terminated
     * The parameter of the event is the close code for the WS connection
     * it indicates whether or not the authentication token needs to be recreated
     */
    onClosed(code) {
        // Stop the keep alive
        if (this._pingClock) clearInterval(this._pingClock);

        // reconnect, control reauthentication based on error code
        switch (code) {
            // Authentication issue
            case 4401:
            case 4407:
            case 1011:
                this.reconnectWithDelay(false); // regenerate token
                break;
            // Non-authentication issue
            default:
                this.reconnectWithDelay(true); // do not regenerate token
                break;
        }
    }


    reconnectWithDelay(skipTokenGeneration = false) {
        // attempt reconnect after 10 seconds
        setTimeout(() => {
            this.reconnect(skipTokenGeneration);
        }, 10000);
    }
}
Clone this wiki locally