SocketManager

interface SocketManager

Socket manager for handling real-time communication with MediaSFU servers.

This interface defines the contract for managing Socket.IO connections, including connection lifecycle, event emission, and event registration.

Usage:

val socketManager: SocketManager = SocketManagerImpl()

// Connect to server
socketManager.connect("https://mediasfu.com", SocketConfig())

// Register event handlers
socketManager.on("message") { data ->
}

// Emit events
socketManager.emit("sendMessage", mapOf("text" to "Hello"))

// Disconnect
socketManager.disconnect()

Inheritors

SocketManagerImpl

Properties

Link copied to clipboard
abstract val id: String?

Unique identifier of the underlying socket connection, when available.

Functions

Link copied to clipboard
abstract suspend fun connect(url: String, config: SocketConfig = SocketConfig()): Result<Unit>

Connect to a Socket.IO server.

Link copied to clipboard
abstract suspend fun disconnect(): Result<Unit>

Disconnect from the Socket.IO server. Cleans up all event handlers and closes the connection.

Link copied to clipboard
abstract suspend fun emit(event: String, data: Map<String, Any?>)

Emit an event to the server without waiting for acknowledgment.

Link copied to clipboard
abstract fun emitWithAck(event: String, data: Map<String, Any?>, callback: (Any?) -> Unit)

Emit an event and invoke callback when the acknowledgment arrives.

abstract suspend fun <T> emitWithAck(event: String, data: Map<String, Any?>, timeout: Long = 5000): T

Emit an event and wait for acknowledgment from the server.

Link copied to clipboard

Get the current connection state.

Link copied to clipboard
abstract fun isConnected(): Boolean

Check if currently connected to the server.

Link copied to clipboard
abstract fun off(event: String)

Unregister a handler for a specific event.

Link copied to clipboard
abstract fun offAll()

Remove all event handlers.

Link copied to clipboard
abstract fun on(event: String, handler: suspend (Map<String, Any?>) -> Unit)

Register a handler for a specific event. The handler will be called whenever the event is received from the server.

Link copied to clipboard
abstract fun onConnect(handler: suspend () -> Unit)

Register a handler for connection events. Called when the socket successfully connects.

Link copied to clipboard
abstract fun onDisconnect(handler: suspend (String) -> Unit)

Register a handler for disconnection events. Called when the socket disconnects (either intentionally or due to error).

Link copied to clipboard
abstract fun onError(handler: suspend (Throwable) -> Unit)

Register a handler for error events. Called when a connection or communication error occurs.

Link copied to clipboard
abstract fun onReconnect(handler: suspend (Int) -> Unit)

Register a handler for reconnection events. Called when the socket successfully reconnects after a disconnection.

Link copied to clipboard
abstract fun onReconnectAttempt(handler: suspend (Int) -> Unit)

Register a handler for reconnection attempts. Called before each reconnection attempt.

Link copied to clipboard
abstract fun onReconnectFailed(handler: suspend () -> Unit)

Register a handler for reconnection failures. Called when all reconnection attempts have been exhausted.