import ballerina/io;
import ballerina/websocket;

public function main() returns error? {
    // Create a new [WebSocket client](https://ballerina.io/swan-lake/learn/api-docs/ballerina/#/ballerina/websocket/latest/clients/Client).
   websocket:Client wsClient = check new("ws://echo.websocket.org");

   // Write a text message to the server using [writeTextMessage](https://ballerina.io/swan-lake/learn/api-docs/ballerina/#/ballerina/websocket/latest/clients/Client#writeTextMessage).
   check wsClient->writeTextMessage("Text message");

   // Read a text message echoed from the server using [readTextMessage](https://ballerina.io/swan-lake/learn/api-docs/ballerina/#/ballerina/websocket/latest/clients/Client#readTextMessage).
   string textResp = check wsClient->readTextMessage();
   io:println(textResp);
}

Handle Text Messages with Client

The WebSocket client can be used to connect to and interact with a WebSocket server in a Synchronous manner. This example demonstrates how to read and write text messages using ballerina websocket client

For more information on the underlying module, see the WebSocket module.

import ballerina/io;
import ballerina/websocket;
public function main() returns error? {
   websocket:Client wsClient = check new("ws://echo.websocket.org");

Create a new WebSocket client.

   check wsClient->writeTextMessage("Text message");

Write a text message to the server using writeTextMessage.

   string textResp = check wsClient->readTextMessage();
   io:println(textResp);
}

Read a text message echoed from the server using readTextMessage.

bal run websocket_text_client.bal
Text message