$connection
Updated on Aug 21, 2025 3 minutes to readThe Connection Plugin provides a unified interface to create, manage, and interact with different connection protocols.
Methods
Method | Description |
---|---|
WebSocket | Creates a new WebSocket client. |
Methods Details
WebSocket()
• Type
(url: string) => WebSocketClient
• Details
Expects a WebSocket server URL.
Returns a WebSocketClient instance.
• Example
// Create a new WebSocket client
const wsClient = E8App.$connection.WebSocket("wss://example.com/ws");
// Listen for successful connection
wsClient.addEventListener("connect", (event) => {
console.log("Connected!", event.detail.session);
// Send a greeting message to other sessions
wsClient.publish({ type: "hello", text: "Hello from client!" });
});
// Listen for incoming messages
wsClient.addEventListener("message", (event) => {
const { from, data } = event.detail;
console.log(`Message from ${from.id}:`, data);
});
// Listen for RPC calls from other sessions
wsClient.addEventListener("call", (event) => {
const { from, params, callback } = event.detail;
console.log(`Call from ${from.id} with params:`, params);
// Respond to the calling session
callback({ ok: true, echo: params });
});
// Listen for disconnection
wsClient.addEventListener("disconnect", () => {
console.log("Disconnected!");
});
// Listen for errors
wsClient.addEventListener("error", (event) => {
console.error("WebSocket error:", event.detail);
});
// Connect to the WebSocket server
wsClient.connect();
// Example of making an RPC call to another session
async function exampleCall(sessionId) {
try {
const response = await wsClient.call(sessionId, { action: "ping" });
console.log("Response from remote session:", response);
} catch (err) {
console.error("RPC call failed:", err);
}
}