Swan Lake Beta2 is here.
import ballerina/websocket;

// A WebSocket listener can be configured to communicate through WSS as well.
// To secure an listener using SSL/TLS, the listener needs to be configured with
// a certificate file and a private key file for the listener.
// The [`websocket:ListenerSecureSocket`](https://docs.central.ballerina.io/ballerina/websocket/latest/records/ListenerSecureSocket) record
// provides the SSL-related listener configurations of the listener.
listener websocket:Listener securedEP = new(9090,
    secureSocket = {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
);

service /foo on securedEP {
    resource isolated function get bar() returns websocket:Service {
        return new WsService();
   }
}

service class WsService {
    *websocket:Service;
    remote isolated function onTextMessage(websocket:Caller caller,
                             string text) returns websocket:Error? {
        check caller->writeTextMessage(text);
    }
}

Service - SSL/TLS

You can use the WebSocket listener to connect to or interact with an WebSocket client. Provide the websocket:ListenerSecureSocket configurations to the server to expose an WSS connection.

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

import ballerina/websocket;
listener websocket:Listener securedEP = new(9090,
    secureSocket = {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
);

A WebSocket listener can be configured to communicate through WSS as well. To secure an listener using SSL/TLS, the listener needs to be configured with a certificate file and a private key file for the listener. The websocket:ListenerSecureSocket record provides the SSL-related listener configurations of the listener.

service /foo on securedEP {
    resource isolated function get bar() returns websocket:Service {
        return new WsService();
   }
}
service class WsService {
    *websocket:Service;
    remote isolated function onTextMessage(websocket:Caller caller,
                             string text) returns websocket:Error? {
        check caller->writeTextMessage(text);
    }
}
# You may need to change the certificate file path and private key file path.
bal run websocket_service_ssl_tls.bal
[ballerina/websocket] started WSS listener 0.0.0.0:9090
import ballerina/websocket;

// A WebSocket listener can be configured to communicate through WSS as well.
// To secure an listener using SSL/TLS, the listener needs to be configured with
// a certificate file and a private key file for the listener.
// The [`websocket:ListenerSecureSocket`](https://docs.central.ballerina.io/ballerina/websocket/latest/records/ListenerSecureSocket) record
// provides the SSL-related listener configurations of the listener.
listener websocket:Listener securedEP = new(9090,
    secureSocket = {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
);

service /foo on securedEP {
    resource isolated function get bar() returns websocket:Service {
        return new WsService();
   }
}

service class WsService {
    *websocket:Service;
    remote isolated function onTextMessage(websocket:Caller caller,
                             string text) returns websocket:Error? {
        check caller->writeTextMessage(text);
    }
}

Service - SSL/TLS

You can use the WebSocket listener to connect to or interact with an WebSocket client. Provide the websocket:ListenerSecureSocket configurations to the server to expose an WSS connection.

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

import ballerina/websocket;
listener websocket:Listener securedEP = new(9090,
    secureSocket = {
        key: {
            certFile: "../resource/path/to/public.crt",
            keyFile: "../resource/path/to/private.key"
        }
    }
);

A WebSocket listener can be configured to communicate through WSS as well. To secure an listener using SSL/TLS, the listener needs to be configured with a certificate file and a private key file for the listener. The websocket:ListenerSecureSocket record provides the SSL-related listener configurations of the listener.

service /foo on securedEP {
    resource isolated function get bar() returns websocket:Service {
        return new WsService();
   }
}
service class WsService {
    *websocket:Service;
    remote isolated function onTextMessage(websocket:Caller caller,
                             string text) returns websocket:Error? {
        check caller->writeTextMessage(text);
    }
}
# You may need to change the certificate file path and private key file path.
bal run websocket_service_ssl_tls.bal
[ballerina/websocket] started WSS listener 0.0.0.0:9090

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