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);
        return 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);
        return 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