Skip to main content

TitanSocket

TitanSocket

A kotlin multiplatform library to manage sockets with support for both iOS & Android

You can find the latest package here from MVN

How it works

TitanSocket handles all the websocket connections, ping & pong between the client & server, and the event notifications for when data is received or broadcasted, or any connectivity status changes happen.

To get started, import the library into your project:


_1
implementation("io.github.thearchitect123:titansocket:+") // latest package

To use TitanSocket, generate an instance of your socket, pass the Url + any Post Connection Logic, and subscribe to the states you wish to connect to:


_8
val socketConnection = TitanSocket("wss://mysupersecret/websocket") {
_8
subscribeOn(TitanSocketEvents.CONNECTION_OPENED) {}
_8
subscribeOn(TitanSocketEvents.DISCONNECTION) {}
_8
subscribeOn(TitanSocketEvents.FAILURE) {}
_8
subscribeOn(TitanSocketEvents.MESSAGE_BINARY_RECEIVED) {}
_8
subscribeOn(TitanSocketEvents.MESSAGE_RECEIVED) {}
_8
subscribeOn(TitanSocketEvents.MESSAGE_SENDING) {}
_8
})

Connect your telemetry Endpoints into TitanSocket to listen to Ping & Pong events.


_15
val socketConnection = TitanSocket("wss://mysupersecret/websocket") {
_15
subscribeOn(TitanSocketEvents.CONNECTION_OPENED) {}
_15
subscribeOn(TitanSocketEvents.DISCONNECTION) {}
_15
subscribeOn(TitanSocketEvents.FAILURE) {}
_15
subscribeOn(TitanSocketEvents.MESSAGE_BINARY_RECEIVED) {}
_15
subscribeOn(TitanSocketEvents.MESSAGE_RECEIVED) {}
_15
subscribeOn(TitanSocketEvents.MESSAGE_SENDING) {}
_15
}) {
_15
onReceiveResponseWebSocket() {
_15
println("RESPONSE $it")
_15
}
_15
onSendRequestWebSocket() {
_15
println("REQUEST $it")
_15
}
_15
}

After your Socket is configured, startup the connection.


_1
socketConnection.connectSocket()

Make sure to close your web socket connection after you are done with it to avoid battery issues.


_1