# IMessageQueue.subscribe() method · @imqueue/core

Source: https://imqueue.org/api/core/latest/core.imessagequeue.subscribe/
Published: 2026-08-04
Author: @imqueue maintainers (https://github.com/imqueue)
Package: @imqueue/core 3.3.2 — generated reference, not hand-written

Subscribes to the pub/sub channel with the given name and registers a handler for the data it delivers. The effective channel is `<prefix>:<channel>`.

**Signature:**

```typescript
subscribe(channel: string, handler: (data: JsonObject) => void): Promise<void>;
```

## Parameters


| Parameter | Type | Description |
| --- | --- | --- |
| channel | string | channel name within the queue's prefix namespace |
| handler | (data: [JsonObject](https://imqueue.org/api/core/latest/core.jsonobject/)) => void | invoked with the parsed payload of each published message |


**Returns:**

Promise<void>

## Exceptions

TypeError when no channel name is given, or when a different channel name is supplied while a subscription is already open — an instance supports exactly one channel until [IMessageQueue.unsubscribe()](https://imqueue.org/api/core/latest/core.imessagequeue.unsubscribe/) resets it

## Remarks

Calling this repeatedly with the same channel name adds another handler rather than replacing the existing one, and all of them are invoked. The subscription uses its own connection, so it does not require [IMessageQueue.start()](https://imqueue.org/api/core/latest/core.imessagequeue.start/), and it is re-established automatically after a reconnect.

Payloads are always plain JSON — [IMQOptions.useGzip](https://imqueue.org/api/core/latest/core.imqoptions.usegzip/) does not apply to pub/sub. Delivery is fire-and-forget: messages published while nobody is subscribed are lost, unlike queued messages.

