# XGROUP

> Manage consumer groups for Redis streams.

## Arguments

<ParamField body="key" type="string" required>
  The key of the stream.
</ParamField>

<ParamField body="subcommand" type="object" required>
  The XGROUP subcommand and its parameters. Can be one of:
  
  <Expandable title="CREATE">
    <ParamField body="type" type="'CREATE'" required>
      Create a new consumer group.
    </ParamField>
    <ParamField body="group" type="string" required>
      The consumer group name.
    </ParamField>
    <ParamField body="id" type="string | '$'" required>
      The stream entry ID to start consuming from. Use '$' to start from the end.
    </ParamField>
    <ParamField body="options">
      <Expandable title="options">
        <ParamField body="MKSTREAM" type="boolean">
          Create the stream if it doesn't exist.
        </ParamField>
        <ParamField body="ENTRIESREAD" type="number">
          Set the number of entries read by the group.
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>

  <Expandable title="CREATECONSUMER">
    <ParamField body="type" type="'CREATECONSUMER'" required>
      Create a new consumer in the group.
    </ParamField>
    <ParamField body="group" type="string" required>
      The consumer group name.
    </ParamField>
    <ParamField body="consumer" type="string" required>
      The consumer name to create.
    </ParamField>
  </Expandable>

  <Expandable title="DELCONSUMER">
    <ParamField body="type" type="'DELCONSUMER'" required>
      Delete a consumer from the group.
    </ParamField>
    <ParamField body="group" type="string" required>
      The consumer group name.
    </ParamField>
    <ParamField body="consumer" type="string" required>
      The consumer name to delete.
    </ParamField>
  </Expandable>

  <Expandable title="DESTROY">
    <ParamField body="type" type="'DESTROY'" required>
      Delete the entire consumer group.
    </ParamField>
    <ParamField body="group" type="string" required>
      The consumer group name to destroy.
    </ParamField>
  </Expandable>

  <Expandable title="SETID">
    <ParamField body="type" type="'SETID'" required>
      Set the last delivered ID for the group.
    </ParamField>
    <ParamField body="group" type="string" required>
      The consumer group name.
    </ParamField>
    <ParamField body="id" type="string | '$'" required>
      The stream entry ID to set as the last delivered ID.
    </ParamField>
    <ParamField body="options">
      <Expandable title="options">
        <ParamField body="ENTRIESREAD" type="number">
          Set the number of entries read by the group.
        </ParamField>
      </Expandable>
    </ParamField>
  </Expandable>
</ParamField>

## Response

<ResponseField>
  The return type depends on the subcommand:
  - CREATE: Returns "OK" string
  - CREATECONSUMER: Returns 1 if created, 0 if already exists
  - DELCONSUMER: Returns the number of pending messages the consumer had
  - DESTROY: Returns 1 if destroyed, 0 if group didn't exist
  - SETID: Returns "OK" string
</ResponseField>

<RequestExample>
```ts Create group
const result = await redis.xgroup("mystream", {
  type: "CREATE",
  group: "mygroup",
  id: "$",
  options: { MKSTREAM: true }
});
```

```ts Create consumer
const result = await redis.xgroup("mystream", {
  type: "CREATECONSUMER",
  group: "mygroup",
  consumer: "consumer1"
});
```

```ts Delete consumer
const result = await redis.xgroup("mystream", {
  type: "DELCONSUMER", 
  group: "mygroup",
  consumer: "consumer1"
});
```

```ts Set group ID
const result = await redis.xgroup("mystream", {
  type: "SETID",
  group: "mygroup", 
  id: "0-0"
});
```

```ts Destroy group
const result = await redis.xgroup("mystream", {
  type: "DESTROY",
  group: "mygroup"
});
```
</RequestExample>
