WebSocket client

WebSocket client is defined by WebSocket interface. It looks like this:

interface WebSocket {
    suspend fun send(message: String)
    fun subscribe(): Flow<String>
    fun subscribeToErrors(): Flow<Throwable>
}

Basically, it has two main functionalities: sending a message and subscribing to the responses (and/or errors).

And here is how WebSocket client itself looks like:

internal class WebSocketClient(
    secure: Boolean = false,
    host: String,
    path: String? = null,
    params: Map<String, Any?> = mapOf(),
    port: Int? = null,
    private val policy: WebSocketClientSubscriptionPolicy = WebSocketClientSubscriptionPolicy.NONE
): WebSocket

When creating the client we set the whether it should be secure or not (if secure, uses wss); its host, path, parameters, port. The last property is the policy that should be applied to the client.

It's defined like this:

enum class WebSocketClientSubscriptionPolicy {
    NONE,
    FIRST_SUBSCRIBER,
    ALL_SUBSCRIBERS
}t

The policy can be one of the following three types:

  1. NONE - No future subscriber recieves the pending data

  2. FIRST_SUBSCRIBER - Only the first subscriber recieves pending data

  3. ALL_SUBSCRIBERS - All subscribers recieve pending data

The default policy is set to NONE.

Last updated