4.8.2 • Published 10 months ago

@cognigy/socket-client v4.8.2

Weekly downloads
27
License
SEE LICENSE IN LI...
Repository
-
Last release
10 months ago

Cognigy Socket Client

This package is used to create a connection to Cognigy.AI via a Socket Endpoint.
You can read about setting up a Socket Endpoint in our platform documentation

Installation

Install this module using the following npm command

npm install @cognigy/socket-client

Usage

const { SocketClient } = require("@cognigy/socket-client");

(async () => {
  // create a client instance with a socket url and an url token
  const client = new SocketClient("https://socket.url", "socket-token", {
    // if you use node, internet explorer or safari, you need to enforce websockets
    forceWebsockets: true,
  });

  // register a handler for messages
  client.on("output", (output) => {
    console.log("Text: " + output.text + "   Data: " + output.data);
  });

  // establish a socket connection (returns a promise)
  await client.connect();

  // send a message with text, text and data, data only
  client.sendMessage("hello there");
  client.sendMessage("hello there", { color: "green" });
  client.sendMessage("", { color: "green" });
})();

In case you are using TypeScript in a frontend codebase, you may need to manually install @types/node to avoid transpilation errors regarding event-related code parts like e.g. client.on()

Compatibility

SocketClientCognigy.AINote
>= v4.8.1>= v4.79.0Starting with SocketClient version v4.8.1, the client waits for an "endpoint-ready" event during connect() which is only emitted by Endpoints from Cognigy.AI version v4.79.0 or later!
<= v4.8.0*

Socket Events

You can subscribe to the following events from the SocketClient:

client.on("finalPing", () => {
  console.log("bot is done processing a message");
});
NameEvent PayloadDescription
output{ text, data }fires on every incoming message from the bot
typingStatus"on" or "off"fires when the typing indicator should show or hide
finalPing-fires when the bot is done processing a message
error{ message }fires when an error happened in the bot

Options

You can pass a third argument to SocketClient to set additional options as follows:

const client = new SocketClient("https://socket.url", "socket-token", {
  userId: "user1234",
});
NameTypeDefaultDescription '
userIdstringrandom stringthe user id for the conversation
sessionIdstringrandom stringthe session id for the conversation
channelstring"socket-client"the name of the channel (can be used for analytics purposes)
forceWebsocketsbooleanauto-determined by runtime-environmentIf this is enabled, the client will only use websockets and not fall back to http polling (wins over disableWebsockets)
disableWebsocketsbooleanfalseIf this is enabled, the client will only use http polling and will not try to upgrade to websockets
intervalnumber10000the interval for polling if in http polling fallback
reconnectionbooleantrueif enabled, will try to reconnect if the connection is aborted
reconnectionLimitnumber5limit the maximum number of reconnection attempts, 0 means no limit
enableInnerSocketHandshakebooleanfalseIf this is enabled, the session parameters (userId, sessionId, urlToken) will be transferred through the websocket instead of query params
5.0.0-beta.21

10 months ago

4.8.2

10 months ago

5.0.0-beta.20

11 months ago

4.8.1

1 year ago

5.0.0-beta.15

1 year ago

5.0.0-beta.16

1 year ago

5.0.0-beta.19

11 months ago

5.0.0-beta.17

1 year ago

5.0.0-beta.18

11 months ago

4.8.0

1 year ago

5.0.0-beta.14

1 year ago

5.0.0-beta.13

1 year ago

5.0.0-beta.11

1 year ago

5.0.0-beta.12

1 year ago

3.0.0-beta.12

1 year ago

5.0.0-beta.8

2 years ago

5.0.0-beta.9

2 years ago

5.0.0-beta.10

2 years ago

5.0.0-beta.7

2 years ago

5.0.0-beta.6

2 years ago

5.0.0-beta.5

2 years ago

5.0.0-beta.4

2 years ago

5.0.0-beta.2

2 years ago

5.0.0-beta.3

2 years ago

5.0.0-beta.1

2 years ago

4.7.1

2 years ago

4.7.0

3 years ago

4.6.3

3 years ago

4.6.1

3 years ago

4.6.0

3 years ago

4.5.5

4 years ago

4.5.4

4 years ago

4.5.3

4 years ago

4.5.2

4 years ago

4.5.1

4 years ago

4.5.0

4 years ago

4.4.0

5 years ago

4.3.1

5 years ago

4.3.0

5 years ago

4.2.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.0

6 years ago