Swan Lake Beta2 is here.
import ballerina/io;
import ballerina/lang.'string;
import ballerina/websocket;

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

   // Write a binary message to the server using [writeBinaryMessage](https://docs.central.ballerina.io/ballerina/websocket/latest/clients/Client#writeBinaryMessage).
   check wsClient->writeBinaryMessage("Binary message".toBytes());

   // Read a binary message echoed from the server using [readBinaryMessage](https://docs.central.ballerina.io/ballerina/websocket/latest/clients/Client#readBinaryMessage).
   byte[] byteResp = check wsClient->readBinaryMessage();
   string stringResp = check 'string:fromBytes(byteResp);
   io:println(stringResp);
}

Handle Binary 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 binary messages using ballerina websocket client

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

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

Create a new WebSocket client.

   check wsClient->writeBinaryMessage("Binary message".toBytes());

Write a binary message to the server using writeBinaryMessage.

   byte[] byteResp = check wsClient->readBinaryMessage();
   string stringResp = check 'string:fromBytes(byteResp);
   io:println(stringResp);
}

Read a binary message echoed from the server using readBinaryMessage.

bal run websocket_binary_client.bal
Binary message
import ballerina/io;
import ballerina/lang.'string;
import ballerina/websocket;

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

   // Write a binary message to the server using [writeBinaryMessage](https://docs.central.ballerina.io/ballerina/websocket/latest/clients/Client#writeBinaryMessage).
   check wsClient->writeBinaryMessage("Binary message".toBytes());

   // Read a binary message echoed from the server using [readBinaryMessage](https://docs.central.ballerina.io/ballerina/websocket/latest/clients/Client#readBinaryMessage).
   byte[] byteResp = check wsClient->readBinaryMessage();
   string stringResp = check 'string:fromBytes(byteResp);
   io:println(stringResp);
}

Handle Binary 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 binary messages using ballerina websocket client

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

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

Create a new WebSocket client.

   check wsClient->writeBinaryMessage("Binary message".toBytes());

Write a binary message to the server using writeBinaryMessage.

   byte[] byteResp = check wsClient->readBinaryMessage();
   string stringResp = check 'string:fromBytes(byteResp);
   io:println(stringResp);
}

Read a binary message echoed from the server using readBinaryMessage.

bal run websocket_binary_client.bal
Binary message

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