kmp-web-sockets

Lightweight WebSocket client with connection manager, coroutine-friendly incoming message stream, easy connect/send/disconnect, and simple integration for reactive UI state and view models.

Android JVMJVMKotlin/Native
GitHub stars0
Open issues5
LicenseMIT License
Creation date4 months ago

Last activity2 months ago
Latest release1.0.0 (4 months ago)

Wannaverse Logo

Build iOS Build Linux

Web Socket Client

A Kotlin Multiplatform web socket library for Android, iOS and Desktop.

✅ Supported Platform

Platform Supported
Android ✔️
iOS ✔️
Desktop ✔️
Web ❌️

🚀 Installation

See the releases section of this repository for the latest version.

To your build.gradle under commonMain.dependencies add:

implementation "com.wannaverse:kmp-web-sockets:<version>"

Usage

KDocs

Below is a sample code that you may use.

Example ViewModel:

class AppViewModel : ViewModel() {
    private var wsManager: WebSocketManager? = null

    var isLoading = mutableStateOf(false)
    var connected = mutableStateOf(false)
    val websocketURL = mutableStateOf("wss://ws.ifelse.io")
    val messages = mutableStateListOf<String>()

    fun connectToServer() = viewModelScope.launch {
        isLoading.value = true

        if (wsManager == null) {
            wsManager = WebSocketManager(websocketURL.value)
            wsManager?.incomingMessages?.onEach { message ->
                messages.add("Server: $message")
            }?.launchIn(viewModelScope)
        }

        wsManager?.connect()

        isLoading.value = false
        connected.value = true
    }

    fun sendMessage(message: String) {
        messages.add("You: $message")
        wsManager?.send(message)
    }

    fun disconnect() {
        connected.value = false
        wsManager?.disconnect()
    }

    override fun onCleared() {
        connected.value = false
        super.onCleared()
        wsManager?.clear()
    }
}

📄 License

MIT LICENSE. See LICENSE for details.

🙌 Contributing

Pull requests and feature requests are welcome! If you encounter any issues, feel free to open an issue.

Android JVMJVMKotlin/Native
GitHub stars0
Open issues5
LicenseMIT License
Creation date4 months ago

Last activity2 months ago
Latest release1.0.0 (4 months ago)

Wannaverse Logo

Build iOS Build Linux

Web Socket Client

A Kotlin Multiplatform web socket library for Android, iOS and Desktop.

✅ Supported Platform

Platform Supported
Android ✔️
iOS ✔️
Desktop ✔️
Web ❌️

🚀 Installation

See the releases section of this repository for the latest version.

To your build.gradle under commonMain.dependencies add:

implementation "com.wannaverse:kmp-web-sockets:<version>"

Usage

KDocs

Below is a sample code that you may use.

Example ViewModel:

class AppViewModel : ViewModel() {
    private var wsManager: WebSocketManager? = null

    var isLoading = mutableStateOf(false)
    var connected = mutableStateOf(false)
    val websocketURL = mutableStateOf("wss://ws.ifelse.io")
    val messages = mutableStateListOf<String>()

    fun connectToServer() = viewModelScope.launch {
        isLoading.value = true

        if (wsManager == null) {
            wsManager = WebSocketManager(websocketURL.value)
            wsManager?.incomingMessages?.onEach { message ->
                messages.add("Server: $message")
            }?.launchIn(viewModelScope)
        }

        wsManager?.connect()

        isLoading.value = false
        connected.value = true
    }

    fun sendMessage(message: String) {
        messages.add("You: $message")
        wsManager?.send(message)
    }

    fun disconnect() {
        connected.value = false
        wsManager?.disconnect()
    }

    override fun onCleared() {
        connected.value = false
        super.onCleared()
        wsManager?.clear()
    }
}

📄 License

MIT LICENSE. See LICENSE for details.

🙌 Contributing

Pull requests and feature requests are welcome! If you encounter any issues, feel free to open an issue.