You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 2, 2023. It is now read-only.
This functionshould be called in the first stage of an agent lifecycle, or when an agent is disposed.
781
+
782
+
If you happen to call [dispose](#dispose), but you want to re-connect the agent again, use this function.
783
+
784
+
This method requires you to provide a callback functionto check if an error is encountered.
785
+
786
+
An example would be:
787
+
```javascript
788
+
agent.connect((err) => {
789
+
if (err) {
790
+
console.error('An error occurred while connecting agent.', err);
791
+
}
792
+
});
793
+
```
794
+
775
795
#### reconnect(skipTokenGeneration)
776
796
**Make sure that you implement reconnect logic according to [liveperson's retry policy guidelines](https://developers.liveperson.com/guides-retry-policy.html)**
777
797
@@ -783,6 +803,28 @@ Call `reconnect` on `error` with code `401`.
783
803
784
804
**Note**: When the `reconnect` method fails to re-establish a connection with LiveEngage, a `closed` and `error` events will fire. Unless these events are handled, multiple instances of a reconnection mechanism will be triggered. See our (retry policy)[https://developers.liveperson.com/retry-and-keepalive-best-practices-overview.html] for more information on how we recommend you handle a retry mechanism.
785
805
806
+
#### getBearerToken()
807
+
After you connect an agent successfully, you may use this method to get the bearer token of an agent to call other APIs within LivePerson services.
808
+
809
+
#### refreshSession(callback)
810
+
Use this method to prolong the session of the agent. In another note, this method prolongs the lifetime of the bearer token.
811
+
812
+
This method requires you to provide a callback function to check if an error is encountered.
813
+
814
+
An example would be:
815
+
```javascript
816
+
agent.refreshSession((err) => {
817
+
if (err) {
818
+
console.error('An error occurred while refreshing agent session.', err);
819
+
}
820
+
});
821
+
```
822
+
823
+
### startPeriodicRefreshSession()
824
+
Use this method to restart the refreshSession periodic calls to make sure that the bearer token is valid forever.
825
+
826
+
This method will also be called when you reconnect with token generation.
827
+
786
828
#### dispose()
787
829
Will dispose of the connection and unregister internal events.
788
830
@@ -1303,34 +1345,52 @@ agent.on('notification', body => {});
1303
1345
```
1304
1346
1305
1347
#### closed
1306
-
This event fires when the socket is closed. If the reason is code 4401 or 4407 this indicates an authentication issue, so when you call [reconnect()](#reconnect(skiptokengeneration)) you should make sure not to pass the `skipTokenGeneration` argument.
1348
+
This event fires when the socket is closed. If the reason is code 4401, 4407, or 1011 this indicates an authentication issue, so when you call [reconnect()](#reconnect(skiptokengeneration)) you should make sure not to pass the `skipTokenGeneration` argument.
1349
+
1350
+
In any other case, please make sure to [reconnect()](#reconnect(skiptokengeneration)) with passing the skipTokenGeneration flag set to true to avoid token re-generation.
1307
1351
1308
1352
This event will only occur once, so if you want to attempt to reconnect repeatedly you should initiate a periodic reconnect attempt here. **LivePerson recommends that you make periodic reconnect attempts at increasing intervals up to a finite number of attempts in order to prevent flooding our service and being blocked as a potentially abusive client**. See [LivePerson's retry policy guidelines](https://developers.liveperson.com/guides-retry-policy.html) for more information.
1309
1353
1310
1354
In the sample below we attempt to reconnect 35 times, waiting 5 seconds the first time and increasing the interval by a factor of 1.25 between each attempt.
1311
1355
1312
-
Sample code:
1356
+
#### Reconnect with Retry
1357
+
1313
1358
```javascript
1314
1359
const reconnectInterval = 5; // in seconds
1315
1360
const reconnectAttempts = 35;
1316
1361
const reconnectRatio = 1.25; // ratio in the geometric series used to determine reconnect exponential back-off
if (++attempt <= reconnectAttempts) { agent._reconnect(delay * reconnectRatio, attempt) }
1332
-
}, delay * 1000)
1333
-
}
1334
1394
```
1335
1395
1336
1396
Example payload:
@@ -1339,13 +1399,36 @@ Example payload:
1339
1399
```
1340
1400
1341
1401
#### error
1342
-
This event fires when the SDK receives an error from the messaging service. If you receive a `401` error you should [reconnect()](#reconnect) according to the [retry policy guidelines](https://developers.liveperson.com/guides-retry-policy.html) mentioned above, in the [closed](#closed) section.
1402
+
This event fires when the SDK receives an error from the messaging service. There are two parameters that are passed in to the event.
1403
+
1404
+
* error:
1405
+
1406
+
```javascript
1407
+
// The SDKError object
1408
+
{
1409
+
message: 'the message of the error',
1410
+
code: 401, // Error code if the error actually comes from a network call (such as a REST API invocation)
1411
+
error: Error // The original error object if it comes from another error that is not caused by a network call
1412
+
}
1413
+
```
1414
+
1415
+
* context:
1416
+
```javascript
1417
+
{
1418
+
location: 'Event#Source' // The source location of the event, for example 'Reconnect#Login', which happens during the login section of the reconnect function
1419
+
}
1420
+
```
1421
+
1422
+
For more information of the Context object and what to do in times of failures, please visit [Success and Error Events](./README_success_and_error_events.md).
1423
+
1424
+
If you receive a `401` error you should [reconnect()](#reconnect) according to the [retry policy guidelines](https://developers.liveperson.com/guides-retry-policy.html) mentioned above, in the [closed](#closed) section.
1343
1425
1344
1426
Sample code:
1345
1427
```javascript
1346
-
agent.on('error', err => {
1428
+
agent.on('error', (err, context) => {
1347
1429
if (err && err.code === 401) {
1348
-
agent._reconnect(); // agent._reconnect() defined in the on('closed',() => {}) example above.
1430
+
agent._reconnect(); // The reconnect function defined in the closed section above.
1431
+
// This will re-connect the WS connection and re-generate the bearer token
Node Agent SDK emits events when a flow succeeds or fails. These events will be useful in determining action items to take, as well as giving visibility about a particular flow in the SDK.
5
+
6
+
For example, you might want to have some metrics that keep track of how many times 'RefreshSession#Login' flow fails vs succeeding.
7
+
8
+
9
+
## Success
10
+
11
+
Success events provide you with a parameter called context:
12
+
13
+
```javascript
14
+
{
15
+
location:'Event#Source'// The source location of the event, for example 'Reconnect#Login',
16
+
// which happens during the login section of the reconnect function
17
+
}
18
+
```
19
+
20
+
An example of implementation on putting a metric on the success events would be:
There are two parameters that are passed in to the error event.
31
+
32
+
* error:
33
+
34
+
```javascript
35
+
// The SDKError object that centralizes the error sources across the SDK
36
+
{
37
+
message:'the message of the error',
38
+
code:401, // Error code if the error actually comes from a network call (such as a REST API invocation)
39
+
error:Error// The original error object if it comes from another error
40
+
}
41
+
```
42
+
43
+
* context:
44
+
```javascript
45
+
{
46
+
location:'Event#Source'// The source location of the event, for example 'Reconnect#Login', which happens during the login section of the reconnect function
47
+
}
48
+
```
49
+
50
+
An example of implementation of metricizing the event would be:
This indicates that you might be rate-limited or the service is down. Please implement a retry logic with exponential backoff on the [reconnect()](README.md#reconnectskiptokengeneration) function
62
+
**without** token generation. You can copy the `_reconnect` function from the [closed event section](README.md#reconnect-with-retry). For example:
63
+
64
+
```javascript
65
+
// on connected cancel any retry interval remaining from reconnect attempt
66
+
agent.on('connected', () => {
67
+
clearTimeout(agent._retryConnection);
68
+
});
69
+
70
+
agent.on('error', (err, context) => {
71
+
if (err && (err.code===429||err.code>=500)) {
72
+
agent._reconnect(true); // do not re-generate token
73
+
}
74
+
});
75
+
```
76
+
77
+
### Error codes 401 or 403
78
+
79
+
This indicates that your token might be expired. Please implement a retry logic with exponential backoff on the [reconnect()](README.md#reconnectskiptokengeneration) function
80
+
**with** token generation. You can copy the `_reconnect` function from the [closed event section](README.md#reconnect-with-retry). For example:
81
+
82
+
```javascript
83
+
agent.on('error', (err, context) => {
84
+
if (err && (err.code===401||err.code403)) {
85
+
agent._reconnect(); // regenerate token
86
+
}
87
+
});
88
+
```
89
+
90
+
91
+
## Details of the Context Location
92
+
93
+
### Connect#CSDS
94
+
95
+
#### Description
96
+
97
+
This happens when the bot is trying to contact to the LivePerson's domains service when the bot is trying to establish a WebSocket connection
98
+
for the very first time.
99
+
100
+
It happens inside the [connect](README.md#connectcallback) function.
101
+
102
+
#### Failure Cases
103
+
104
+
Please refer to [General Failure Cases](#reconnect-general-failure-cases).
105
+
106
+
### Connect#Login
107
+
108
+
#### Description
109
+
110
+
This happens when the bot is trying to login to LivePerson when the bot is trying to establish a WebSocket connection
111
+
for the very first time.
112
+
113
+
It happens inside the [connect](README.md#connectcallback) function.
114
+
115
+
#### Failure Cases
116
+
117
+
Please refer to [General Failure Cases](#reconnect-general-failure-cases).
118
+
119
+
### Reconnect#CSDS
120
+
121
+
#### Description
122
+
123
+
This happens when the bot is trying to contact to the LivePerson's domains service when the bot is trying to establish a WebSocket connection after it was disconnected.
124
+
125
+
It happens inside the [reconnect(skipTokenGeneration)](README.md#reconnectskiptokengeneration) function.
126
+
127
+
#### Failure Cases
128
+
129
+
Please refer to [General Failure Cases](#reconnect-general-failure-cases).
130
+
131
+
### Reconnect#Relogin#WS
132
+
133
+
#### Description
134
+
135
+
This happens when the bot is trying to login and re-establish a WS connection to LivePerson after the bot was disconnected.
136
+
137
+
It happens inside the [reconnect(skipTokenGeneration)](README.md#reconnectskiptokengeneration) function.
138
+
139
+
#### Failure Cases
140
+
141
+
Please refer to [General Failure Cases](#reconnect-general-failure-cases).
142
+
143
+
### RefreshSession#CSDS
144
+
145
+
This happens when the bot is trying to contact to the LivePerson's domains service when the bot is trying to prolong the agent's session.
146
+
147
+
It happens inside the [refreshSession(callback)](README.md#refreshsessioncallback) function.
148
+
149
+
#### Failure Cases
150
+
151
+
##### Error codes 429 or 5xx for Refresh Sessions
152
+
153
+
This indicates that you might be rate-limited or the service is down. Please implement a retry logic with exponential backoff on the [refreshSession(callback)](README.md#refreshsessioncallback) function.
154
+
155
+
If you want to attempt to reconnect repeatedly you should initiate a periodic reconnect attempt here. **LivePerson recommends that you make periodic reconnect attempts at increasing intervals up to a finite number of attempts in order to prevent flooding our service and being blocked as a potentially abusive client**. See [LivePerson's retry policy guidelines](https://developers.liveperson.com/guides-retry-policy.html) for more information.
156
+
157
+
In the sample below we attempt to reconnect 35 times, waiting 5 seconds the first time and increasing the interval by a factor of 1.25 between each attempt.
158
+
159
+
###### Reconnect with Retry
160
+
161
+
```javascript
162
+
constreconnectInterval=5; // in seconds
163
+
constreconnectAttempts=35;
164
+
constreconnectRatio=1.25; // ratio in the geometric series used to determine reconnect exponential back-off
0 commit comments