# Events (/sdks/kotlin/reference/events)



Use `PubSubClient` for fire-and-forget events. Messages are built with the `eventMessage { }` DSL; subscriptions return a `Flow` of received events.

## publishEvent [#publishevent]

```kotlin
suspend fun publishEvent(message: EventMessage)
```

Publishes an event to the channel embedded in `EventMessage`.

**Parameters:**

| Name      | Type           | Required | Description                      |
| --------- | -------------- | -------- | -------------------------------- |
| `message` | `EventMessage` | Yes      | Built via `eventMessage { }` DSL |

**Throws:** `KubeMQException` -- transport, validation, or auth failures.

## publishEventStream [#publisheventstream]

```kotlin
fun publishEventStream(events: Flow<EventMessage>): Flow<EventSendResult>
```

Streams events for high-throughput publishing. Accepts a `Flow` of messages and returns a `Flow` of results.

## subscribeToEvents [#subscribetoevents]

```kotlin
fun subscribeToEvents(config: EventsSubscriptionConfig.() -> Unit): Flow<EventMessageReceived>
```

Returns a cold `Flow` that subscribes to a channel when collected. Supports optional consumer group for competing consumers.

**Configuration DSL:**

| Property  | Type     | Required | Description                                     |
| --------- | -------- | -------- | ----------------------------------------------- |
| `channel` | `String` | Yes      | Channel to subscribe to (supports `>` wildcard) |
| `group`   | `String` | No       | Consumer group for load balancing               |

## eventMessage DSL [#eventmessage-dsl]

```kotlin
eventMessage {
    channel = "events.orders"
    body = "created".toByteArray()
    metadata = "order-service"
    tags = mapOf("priority" to "high")
}
```

| Property   | Type                  | Description    |
| ---------- | --------------------- | -------------- |
| `channel`  | `String`              | Target channel |
| `body`     | `ByteArray`           | Binary payload |
| `metadata` | `String`              | UTF-8 metadata |
| `tags`     | `Map<String, String>` | Key/value tags |

## Quick Usage [#quick-usage]

```kotlin title="Events.kt"
KubeMQClient.pubSub {
    address = "localhost:50000"
    clientId = "events-demo"
}.use { pub ->
    pub.publishEvent(eventMessage {
        channel = "events.orders"
        body = "created".toByteArray()
    })
}
```

## See Also [#see-also]

* [Events Examples](/sdks/kotlin/how-to/events/)
* [Events pattern](/learn/events/getting-started)
* [Kotlin SDK Getting Started](/sdks/kotlin)
