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



Use `PubSubClient` for fire-and-forget events. Messages are expressed with `EventMessage` builders; subscriptions use `EventsSubscription` to wire channel, group, and callbacks.

## publishEvent [#publishevent]

```java
void publishEvent(EventMessage message) throws KubeMQException
```

Publishes an event to the channel embedded in `EventMessage`. This is the preferred method name.

**Parameters:**

| Name      | Type           | Required | Description                        |
| --------- | -------------- | -------- | ---------------------------------- |
| `message` | `EventMessage` | Yes      | Built via `EventMessage.builder()` |

**Returns:** `void` — events are fire-and-forget; no broker acknowledgement is returned.

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

> **Note:** `sendEventsMessage` is a deprecated alias for `publishEvent` (deprecated since 2.2.0, `forRemoval=true` at v3.0). Prefer `publishEvent`.

## subscribeToEvents [#subscribetoevents]

```java
void subscribeToEvents(EventsSubscription subscription) throws KubeMQException
```

Registers a consumer on a channel with optional consumer group for competing consumers.

**Parameters:**

| Name           | Type                 | Required | Description                        |
| -------------- | -------------------- | -------- | ---------------------------------- |
| `subscription` | `EventsSubscription` | Yes      | Channel, group, delivery callbacks |

**Throws:** `KubeMQException` — subscription cannot be established.

> **Note:** Keep callback bodies non-blocking; move heavy processing to worker threads.

## EventMessage builder [#eventmessage-builder]

| Method                      | Description    |
| --------------------------- | -------------- |
| `.channel(String)`          | Target channel |
| `.body(byte[])`             | Binary payload |
| `.metadata(String)`         | UTF-8 metadata |
| `.tags(Map<String,String>)` | Key/value tags |

## Quick Usage [#quick-usage]

```java title="Events.java"
try (PubSubClient pub = PubSubClient.builder()
        .address("localhost:50000")
        .clientId("events-demo")
        .build()) {
    EventMessage msg = EventMessage.builder()
        .channel("events.orders")
        .body("created".getBytes(StandardCharsets.UTF_8))
        .build();
    pub.publishEvent(msg);
}
```

## See Also [#see-also]

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