Swan Lake Beta3 is here.
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
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
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand