solibo-sdk

Event Bus (Real-time Updates)

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.

Key Features


Accessing the Event Bus

The EventBus is accessible through the SoliboSDK instance.

Kotlin (Native Mobile / JVM)

val eventBus = sdk.eventBus

React (TypeScript)

In React, you can access the eventBus via the useSoliboApi() hook.

import { useSoliboApi } from '@solibo/solibo-query'

function MyComponent() {
  const { eventBus } = useSoliboApi()
  // ...
}

Subscribing to Events

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).

Kotlin Example

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()

React Example

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>
}

Sending Commands

The Event Bus also supports sending commands to the server.

Kotlin Example

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
        )
    )
}

React / TypeScript Example

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)
}

Error Handling

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])

Connection Lifecycle

The SDK manages the connection automatically (connecting on first access and reconnecting on failure). However, you can manually control it if necessary:

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 calling start().