The Solibo SDK includes a built-in EventBus that uses WebSockets to provide real-time updates from the Solibo Home ecosystem. This is used for features like messaging, typing indicators, and live notifications.
eventBus property.The EventBus is accessible through the SoliboSDK instance.
val eventBus = sdk.eventBus
In React, you can access the eventBus via the useSoliboApi() hook.
import { useSoliboApi } from '@solibo/solibo-query'
function MyComponent() {
const { eventBus } = useSoliboApi()
// ...
}
You can subscribe to events by providing a callback to onEvent. This method returns an “unsubscribe” function that should be called when you no longer want to receive updates (e.g., when a component unmounts).
val unsubscribe = sdk.eventBus.onEvent { event ->
// event.payload is of type WebSocketPayload
when (val payload = event.payload) {
is ConversationMessagePayload -> {
println("New message in ${payload.conversationId}: ${payload.message.text}")
}
is ConversationTypingPayload -> {
println("${payload.name} is typing...")
}
else -> println("Received other event: ${payload::class.simpleName}")
}
}
// Later, to stop listening:
// unsubscribe()
In React, use a useEffect hook to manage the lifecycle of your subscription.
import { useEffect } from 'react'
import { useSoliboApi } from '@solibo/solibo-query'
function ChatRoom({ conversationId }) {
const { eventBus } = useSoliboApi()
useEffect(() => {
const unsubscribe = eventBus.onEvent((event) => {
const { payload } = event
// Discriminator-based type checking in JS/TS
if (payload.type === 'solibo.common.infrastructure.websockets.payload.ConversationMessagePayload') {
console.log('New message received:', payload.message.text)
}
})
return () => unsubscribe()
}, [eventBus])
return <div>Listening for chat messages...</div>
}
The Event Bus also supports sending commands to the server.
Use the send suspend function. It accepts an object of type IncomingEventBusMessage (like Ping or Typing).
import no.solibo.oss.sdk.api.gen.models.Typing
suspend fun notifyTyping(sdk: SoliboSDK, isTyping: Boolean) {
sdk.eventBus.send(
Typing(
companyId = 12345L,
conversationId = 67890L,
typing = isTyping
)
)
}
Use the sendJs method, which handles the coroutine scope internally.
import { Typing } from '@solibo/solibo-sdk'
function handleTyping(eventBus, isTyping) {
// You can pass an instance of the generated class
const payload = new Typing(
12345n, // companyId
67890n, // conversationId
isTyping // typing
)
eventBus.sendJs(payload)
}
You can monitor connection errors or decoding issues using onError.
sdk.eventBus.onError { error ->
println("EventBus error occurred: $error")
}
In React:
useEffect(() => {
const unsubscribe = eventBus.onError((error) => {
console.error('EventBus error:', error)
})
return () => unsubscribe()
}, [eventBus])
The SDK manages the connection automatically (connecting on first access and reconnecting on failure). However, you can manually control it if necessary:
eventBus.stop(): Closes the connection and stops reconnection attempts. (Returns a Promise in JS, suspend in Kotlin).eventBus.start(): Manually triggers a connection attempt (usually not needed as it starts automatically on first access).To save battery or data, you might want to call
stop()when your app goes into the background for an extended period and let it reconnect when returning to the foreground by simply accessing the property or callingstart().