ballerina.net.ws package

public struct BinaryFrame

Represents a WebSocket binary frame in Ballerina.

Field Name Data Type Description Default Value
data blob Binary data of the frame
isFinalFragment boolean Check whether this is the final frame. True if the frame is final frame.

public struct ClientConnectorConfig

Configuration struct for WebSocket client connection

Field Name Data Type Description Default Value
subProtocols string[] Negotiable sub protocols for the client
parentConnectionID string Connection ID of the parent connection to which it should be bound to when connecting
customHeaders map Custom headers which should be sent to the server
idleTimeoutInSeconds int Idle timeout of the client. Upon timeout, onIdleTimeout resource in the client service will be triggered (if there is one defined). -1

public struct CloseFrame

Represents a WebSocket close frame in Ballerina.

Field Name Data Type Description Default Value
statusCode int Status code for the reason of the closure of the connection
reason string Reason to close the connection

public struct Connection

Represents a WebSocket connection in ballerina. This include all connection oriented operations.

Field Name Data Type Description Default Value
attributes map Custom user attributes
  • < Connection > closeConnection ( int statusCode , string reason )

    Close the connection

    Parameter Name Data Type Description
    statusCode int Status code for closing the connection
    reason string Reason for closing the connection
  • < Connection > getID ( ) ( string )

    Gets the ID of the WebSocket connection

    Return Variable Data Type Description
    string
  • < Connection > getNegotiatedSubProtocol ( ) ( string )

    Gets the negotiated sub protocol of the connection

    Return Variable Data Type Description
    string
  • < Connection > getParentConnection ( ) ( Connection )

    Gets the parent connection if there is one

    Return Variable Data Type Description
    Connection
  • < Connection > getUpgradeHeader ( string key ) ( string )

    Gets a value of a header

    Parameter Name Data Type Description
    key string Key of the header for which the value should be retrieved

    Return Variable Data Type Description
    string
  • < Connection > getUpgradeHeaders ( ) ( map )

    Gets a map of all the upgrade headers of the connection

    Return Variable Data Type Description
    map
  • < Connection > isOpen ( ) ( boolean )

    Checks whether the connection is still open or not.

    Return Variable Data Type Description
    boolean
  • < Connection > isSecure ( ) ( boolean )

    Checks whether the connection is secure or not

    Return Variable Data Type Description
    boolean
  • < Connection > ping ( blob data )

    Ping the connection

    Parameter Name Data Type Description
    data blob Binary data to be sent
  • < Connection > pong ( blob data )

    Send pong message to the connection

    Parameter Name Data Type Description
    data blob Binary data to be sent
  • < Connection > pushBinary ( blob data )

    Push binary data to the connection

    Parameter Name Data Type Description
    data blob Binary data to be sent
  • < Connection > pushText ( string text )

    Push text to the connection

    Parameter Name Data Type Description
    text string Text to be sent

public struct HandshakeConnection

Represent the details needed before the Handshake is done.

Field Name Data Type Description Default Value
connectionID string ID of the connection
isSecure boolean True if the connection is secured
upgradeHeaders map Received headers in the connection upgrade
  • < HandshakeConnection > cancelHandshake ( int statusCode , string reason )

    Cancels the handshake

    Parameter Name Data Type Description
    statusCode int Status code for closing the connection
    reason string Reason for closing the connection

public struct PingFrame

Represents a WebSocket ping frame in Ballerina.

Field Name Data Type Description Default Value
data blob Data of the frame

public struct PongFrame

Represents a WebSocket pong frame in Ballerina.

Field Name Data Type Description Default Value
data blob Data of the frame

public struct TextFrame

Represents a WebSocket text frame in Ballerina.

Field Name Data Type Description Default Value
text string Text in the text frame
isFinalFragment boolean Check whether this is the final frame. True if the frame is final frame.

public struct WsConnectorError

Error struct for WebSocket connection errors

Field Name Data Type Description Default Value
msg string An error message explaining the error
cause error The error that caused HttpConnectorError to be returned
stackTrace StackFrame[] Represents the invocation stack if WsConnectorError is thrown

public connector WsClient ( string url , string callbackService )

WebSocket client connector for connecting to a WebSocket backend

Parameter Name Data Type Description
url string WebSocket URL for the backend
callbackService string Callback service to listen to the incoming messages from the backend

Actions:

  • < WsClient > connect ( ClientConnectorConfig config ) ( Connection , WsConnectorError )

    Connect to remote endpoint

    Parameter Name Data Type Description
    config ClientConnectorConfig ClientConnectorConfig for the connection

    Return Variable Data Type Description
    Connection
    WsConnectorError
  • < WsClient > connectWithDefault ( ) ( Connection , WsConnectorError )

    Connect to remote endpoint with default configuration

    Return Variable Data Type Description
    Connection
    WsConnectorError

public annotation clientService

Service annotation to mark a WS service as a client service for a WS client connector.

public annotation configuration

Configuration for a WebSocket service.

Attribute Name Data Type Description
basePath string Path of the WebSocket service
subProtocols string[] Negotiable sub protocol by the service
host string Host of the service
port int Port number of the service
wssPort int WSS port number of service
idleTimeoutInSeconds int Idle timeout for the client connection. This can be triggered by putting onIdleTimeout resource in WS service.
keyStoreFile string File path to keystore file
keyStorePassword string The keystore password
certPassword string The certificate password