Skip to main content
The ZapThinker API uses socket.io to emit real-time events, leveraging WebSocket technology. This makes integration development more efficient and straightforward for developers. WebSocket provides a full-duplex communication channel over a single, long-lived connection, allowing real-time data flow between client and server.
To enable WebSockets, set the environment variable WEBSOCKET_ENABLED to true. See more details in Environment Variables.

WebSocket Operation Modes

The WebSocket can only be connected after executing the set command on the instance. This allows the WebSocket to be specific to each instance, and real-time communication is restricted to that instance.
  • Operation: Ideal for scenarios where you want real-time communication focused on a single instance.
  • Connection: Connecting to the WebSocket requires using /instance_name in the URL:
    wss://api.zapthinker.com/instance_name
    

Connecting to WebSocket

Use the following URL format:
wss://api.zapthinker.com/instance_name
Replace instance_name with the name of your specific instance.

Example of Establishing WebSocket Connection

Here’s a basic example of how to establish a WebSocket connection using JavaScript:
const socket = io('wss://api.zapthinker.com/instance_name', {
  transports: ['websocket']
});

socket.on('connect', () => {
  console.log('Connected to ZapThinker API WebSocket');
});

// Listening for events
socket.on('event_name', (data) => {
  console.log('Event received:', data);
});

// Handling disconnection
socket.on('disconnect', () => {
  console.log('Disconnected from ZapThinker API WebSocket');
});
In this example, replace event_name with the specific event you want to listen to.

Event Handling

Once connected, you can listen to various events emitted by the server. Each event can carry data relevant to the event context. For example, if you’re listening for message updates, you might receive data containing the updated message content and metadata.

Closing the Connection

To close the WebSocket connection, use the disconnect method:
socket.disconnect();
Remember to handle the connection responsibly, disconnecting when your application or component is unmounted to prevent memory leaks and ensure efficient use of resources.

Final Considerations

The ZapThinker API offers a powerful way of real-time interaction through WebSockets, providing a seamless experience for both developers and end users. The flexibility of the system allows adaptation to the specific needs of your project.