Swan Lake Beta3 is here.
import ballerina/io;
import ballerina/websocket;

service /basic/ws on new websocket:Listener(9090) {
   resource isolated function get .()
                     returns websocket:Service|websocket:Error {
       // Accept the WebSocket upgrade by returning a `websocket:Service`.
       return new WsService();
   }
}

service class WsService {
    *websocket:Service;
    // This `remote function` is triggered when a new text message is received
    // from a client.
    remote isolated function onTextMessage(websocket:Caller caller,
                                 string text) returns websocket:Error? {
        io:println("\ntext message: " + text);
        check caller->writeTextMessage("You said: " + text);
    }
}

Listener Functionalities

This example explains the basic functions of a WebSocket server.

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

import ballerina/io;
import ballerina/websocket;
service /basic/ws on new websocket:Listener(9090) {
   resource isolated function get .()
                     returns websocket:Service|websocket:Error {
       return new WsService();
   }
}

Accept the WebSocket upgrade by returning a websocket:Service.

service class WsService {
    *websocket:Service;
    remote isolated function onTextMessage(websocket:Caller caller,
                                 string text) returns websocket:Error? {
        io:println("\ntext message: " + text);
        check caller->writeTextMessage("You said: " + text);
    }
}

This remote function is triggered when a new text message is received from a client.

bal run websocket_basic_sample.bal
import ballerina/io;
import ballerina/websocket;

service /basic/ws on new websocket:Listener(9090) {
   resource isolated function get .()
                     returns websocket:Service|websocket:Error {
       // Accept the WebSocket upgrade by returning a `websocket:Service`.
       return new WsService();
   }
}

service class WsService {
    *websocket:Service;
    // This `remote function` is triggered when a new text message is received
    // from a client.
    remote isolated function onTextMessage(websocket:Caller caller,
                                 string text) returns websocket:Error? {
        io:println("\ntext message: " + text);
        check caller->writeTextMessage("You said: " + text);
    }
}

Listener Functionalities

This example explains the basic functions of a WebSocket server.

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

import ballerina/io;
import ballerina/websocket;
service /basic/ws on new websocket:Listener(9090) {
   resource isolated function get .()
                     returns websocket:Service|websocket:Error {
       return new WsService();
   }
}

Accept the WebSocket upgrade by returning a websocket:Service.

service class WsService {
    *websocket:Service;
    remote isolated function onTextMessage(websocket:Caller caller,
                                 string text) returns websocket:Error? {
        io:println("\ntext message: " + text);
        check caller->writeTextMessage("You said: " + text);
    }
}

This remote function is triggered when a new text message is received from a client.

bal run websocket_basic_sample.bal
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