Websocket
Evolution API utilizes socket.io to emit events, leveraging WebSocket technology. This makes the development of integrations more efficient and straightforward for developers. WebSocket provides a full-duplex communication channel over a single, long-lived connection, enabling real-time data flow between the client and server.
WEBSOCKET_ENABLED
as true
. Learn more on Enviroment VariablesConnecting to WebSocket
To connect to the WebSocket server in the Evolution API, you can use the following URL format:
Replace api.yoursite.com with your actual API domain and instance_name with the name of your specific instance.
Example of Establishing a WebSocket Connection Here’s a basic example of how to establish a WebSocket connection using JavaScript:
In this example, replace event_name with the specific event you want to listen to.
Handling Events
Once connected, you can listen for various events emitted by the server. Each event may carry data relevant to the event’s context. For instance, if you’re listening for message updates, you might receive data containing the updated message content and metadata.
Sending Messages
You can also send messages to the server using the emit method:
Closing the Connection
To close the WebSocket connection, use the disconnect method:
Remember to handle the connection responsibly, disconnecting when your application or component unmounts to prevent memory leaks and ensure efficient resource usage.
By leveraging WebSockets, Evolution API offers a powerful way to interact with the system in real time, providing a seamless experience for both developers and end-users.