# Pending Messages

Pending Messages features lets you introduce asynchronous moderation on messages being sent on channel. To use this feature please get in touch with support so that we can enable it for your organisation.

## Sending Pending Messages

Messages can be made pending by default by setting the channel config property `mark_messages_pending` to true.

```swift label="Swift"
// This is a server-side only feature, choose any of our server-side SDKs to use it
```

You can also set the `pending` property on a message to mark it as pending on server side (this will override the channel configuration). **Please note that this is only server-side feature** .

```js label="Node.js"
const channel = client.chat.channel("messaging", "channel-id");

const message = await channel.sendMessage({
  message: {
    text: "this is my pending message",
    user_id: "user-id",
  },
  pending: true,
  pending_message_metadata: {
    my: "metadata",
  },
});
```

Pending messages will only be visible to the user that sent them. They will not be query-able by other users.

## Callbacks

When a pending message is either sent or deleted, the message and its associated pending message metadata are forwarded to your configured callback endpoint via HTTP(s). You may set up to two pending message hooks per application. Only the first commit to a pending message will succeed; any subsequent commit attempts will return an error, as the message is no longer pending. If multiple hooks specify a `timeout_ms`, the system will use the longest timeout value.

The following options are available when configuring a pending message hook:

| Option          | Type   | Description                                                                                                                                  | Required                             |
| --------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
| webhook_url     | string | The URL where pending message events will be sent                                                                                            | Yes, except for `CALLBACK_MODE_NONE` |
| timeout_ms      | number | How long messages should stay pending before being deleted in milliseconds                                                                   | Yes                                  |
| callback.mode   | string | Callback mode ("CALLBACK_MODE_NONE", "CALLBACK_MODE_REST")                                                                                   | Yes                                  |
| failover_config | object | Archives callbacks that fail after all retries to a bucket you own. Requires `CALLBACK_MODE_REST`. See [Failed callbacks](#failed-callbacks) | No                                   |

You can configure this callback using the dashboard or server-side SDKs.

### Using the Dashboard

1. Go to the [Stream Dashboard](https://getstream.io/signin/?product=chat)
2. Select your app
3. Navigate to your app's settings until "Webhook & Event Configuration" section
4. Click on "Add Integration"
5. Add and configure pending message hook

![](https://getstream.io/docs-assets/images/743b4e40dbdb.png)

### Using Server-Side SDKs

```swift label="Swift"
// This is a server-side only feature, choose any of our server-side SDKs to use it
```

See the [Webhooks](https://getstream.io/docs/platform/webhooks/) documentation for complete details.

### Callback Request

For example, if your callback server url is [https://example.com](https://example.com), we would send callbacks:

- When pending message is sent

`POST https://example.com/PassOnPendingMessage`

- When a pending message is deleted

`POST https://example.com/DeletedPendingMessage`

In both callbacks, the body of the POST request will be of the form:

```json label="JSON"
{
  "message": {
    // the message object
  },
  "metadata": {
    // keys and values that you passed as pending_message_metadata
  },
  "request_info": {
    // request info of the request that sent the pending message. Example:
    /*
    "type": "client",
    "ip": "127.0.0.1",
    "user_agent": "Mozilla/5.0...",
    "sdk": "stream-chat-js",
    "ext": "additional-data"
    */
  }
}
```

### Failed callbacks

A callback that still fails after its retries is lost unless the hook has a `failover_config`. With one, the failed callback is written to a Google Cloud Storage or Amazon S3 bucket you own. The archived record holds the message, user, channel, metadata and method, so you can commit or delete each affected message yourself.

Failover requires `callback.mode: "CALLBACK_MODE_REST"`. See [Event failover](https://getstream.io/docs/platform/webhooks/#pending-message-hooks) for the configuration, the bucket permissions and the archived format.

## Deleting pending messages

Pending messages can be deleted using the normal delete message endpoint. Users are only able to delete their own pending messages. The messages must be hard deleted. Soft deleting a pending message will return an error.

## Updating pending messages

Pending messages cannot be updated.

## Querying pending messages

A user can retrieve their own pending messages using the following endpoints:

```js label="JavaScript"
// To retrieve a single message
const response = await client.getMessage({ id: "pending_message_id" });

// To retrieve several messages from one channel
const messages = await channel.getMessagesById([
  "pending_message_id_1",
  "pending_message_id_2",
]);
```

## Query channels

Each channel that is returned from query channels will also have an array of `pending_messages` . These are pending messages that were sent to this channel, and belong to the user who made the query channels call. This array will contain a maximum of 100 messages and these will be the 100 most recently sent messages.

```swift label="Swift"
// Querying multiple channels returns each channel's pending messages
let query = ChannelListQuery(
  filter: .and([
    .equal(.type, to: .messaging),
    .containMembers(userIds: ["thierry"])
  ]),
  sort: [.init(key: .lastMessageAt, isAscending: false)]
)
let channelListController = client.channelListController(query: query)
channelListController.synchronize { error in
  // Pending messages of the first channel
  let pendingMessages = channelListController.channels.first?.pendingMessages
}
```

## Committing pending messages

Calling the commit message endpoint will promote a pending message into a normal message. This message will then be visible to other users and any events/push notifications associated with the message will be sent.

The commit message endpoint is server-side only.

```swift label="Swift"
// This is a server-side only feature, choose any of our server-side SDKs to use it
```

If a message has been in the pending state longer than the `timeout_ms` defined for your app, then the pending message will be deleted. The default timeout for a pending message is 3 days.

---

For the most recent version of this documentation, visit [https://getstream.io/chat/docs/ios-swift/pending-messages/](https://getstream.io/chat/docs/ios-swift/pending-messages/).