[Yandex Cloud documentation](../../../index.md) > [Yandex API Gateway](../../index.md) > [Concepts](../index.md) > [Trigger](index.md) > Trigger for Message Queue

# Trigger for Message Queue that sends messages to WebSocket connections

A [trigger](index.md) for Message Queue speeds up the processing of messages in a [queue](../../../message-queue/concepts/queue.md). It picks up messages from the queue and forwards them to [WebSocket connections](../extensions/websocket.md).

{% note warning %}

* You can only create a trigger for a standard message queue.
* The trigger must be in the same cloud as the queue from which it reads messages.
* You can create only one trigger for each message queue.

{% endnote %}

When the trigger is running, requests to the message queue are free of charge.

A trigger for Message Queue requires [service accounts](../../../iam/concepts/users/service-accounts.md) to read messages from a queue and send them to WebSocket connections. You can use the same service account for both operations.

For more information about creating a trigger for Message Queue, see [Creating a trigger for Message Queue that sends messages to WebSocket connections](../../operations/trigger/ymq-trigger-create.md).

## Message batching {#batching}

The batching settings allow sending several messages to WebSocket connections at a time. They set a top limit on a message batch size and its accumulation time. For example, if the message batch size is 3, WebSocket connections can receive batches containing from 1 to 3 messages.

## Roles required for the proper operation of a trigger for Message Queue {#roles}

* To create a trigger, you need:
   * `viewer` role for a folder with a message queue the trigger reads messages from.
   * `viewer` role for a folder containing the API gateway whose WebSocket connections the trigger sends messages to.
   * Permission for the service account under which the trigger runs the operations. This permission comes with the [iam.serviceAccounts.user](../../../iam/security/index.md#iam-serviceAccounts-user) and [editor](../../../iam/roles-reference.md#editor) roles or higher.
* For the trigger to fire, service accounts need the following roles:
   * `editor` for the folder with a message queue the trigger reads messages from.
   * `api-gateway.websocketBroadcaster` for the folder containing the API gateway.

Read more about [access management](../../security/index.md).

## Message Queue trigger message format {#format}

After the trigger picks up a message from the queue, it will send it to WebSocket connections in the following format:

```json
{
   "messages":[
      {
         "event_metadata":{
            "event_id":"cce76685-5828-4304-a83d-9564********",
            "event_type":"yandex.cloud.events.messagequeue.QueueMessage",
            "created_at":"2019-09-24T00:54:28.980441Z",
            "cloud_id":"b1gvlrnlh2sd********",
            "folder_id":"b1g88tflh2sd********",         
         },
         "details":{
            "queue_id":"yrn:yc:ymq:ru-central1:21i6v06sqmsa********:event-queue",
            "message":{
               "message_id":"cce76685-5828-4304-a83d-9564********",
               "md5_of_body":"d29343907090dff4cec4a9a0********",
               "body":"message body",
               "attributes":{
                  "SentTimestamp":"1569285804456"
               },
               "message_attributes":{
                  "messageAttributeKey":{
                     "data_type":"StringValue",
                     "string_value":"value"
                  }
               },
               "md5_of_message_attributes":"83eb2d0afefb150c1ffe69f6********"
            }
         }
      },
      {
         "event_metadata":{
            "event_id":"1f32fd25-11fc-4c08-88e7-d871********",
            "event_type":"yandex.cloud.events.messagequeue.QueueMessage",
            "created_at":"2019-09-24T00:54:28.980492Z",
            "cloud_id":"b1gvlrnlh2sd********",
            "folder_id":"b1g88tflh2sd********",
         },
         "details":{
            "queue_id":"yrn:yc:ymq:ru-central1:21i6v06sqmsa********:event-queue",
            "message":{
               "message_id":"1f32fd25-11fc-4c08-88e7-d871********",
               "md5_of_body":"d29343907090dff4cec4a9a0********",
               "body":"message body",
               "attributes":{
                  "SentTimestamp":"1569285806456"
               },
               "message_attributes":{
                  "messageAttributeKey":{
                     "data_type":"StringValue",
                     "string_value":"value"
                  }
               },
               "md5_of_message_attributes":"83eb2d0afefb150c1ffe69f6********"
            }
         }
      }
   ]
}
```

## Useful links {#see-also}

* [Trigger for Message Queue that sends messages to a Serverless Containers container](../../../serverless-containers/concepts/trigger/ymq-trigger.md)
* [Trigger for Message Queue that sends messages to a Cloud Functions function](../../../functions/concepts/trigger/ymq-trigger.md)