> For the complete documentation index, see [llms.txt](https://tools.docs.sendbird.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tools.docs.sendbird.com/readme/chat/invite-join-and-leave-channels.md).

# Invite, join, and leave channels

Test two ways a user joins a channel: accepting an invitation to a [private Group channel](/readme/chat.md#terms-used-in-these-tutorials), and joining a [public Group channel](/readme/chat.md#terms-used-in-these-tutorials) without an invitation.

## Before you begin

* Complete [Create test users](/readme/chat/create-test-users.md), and keep terminal A and terminal B open. Commands run against the app that's selected now. If you selected another app since then, [select your test app](/readme/quickstart.md#step-3-select-your-app) again.
* Your Sendbird account's Dashboard role must allow inviting and listing channel members. See [Your account and Chat users](/readme/commands/command-reference.md#your-account-and-chat-users).

## Step 1. Invite Bob to a private channel

In terminal A, create a private Group channel with Alice. `--is-public false` makes the channel private, so users join it only by invitation. `--is-distinct false` creates a new channel every time instead of reusing a [distinct channel](/readme/chat.md#terms-used-in-these-tutorials):

```bash
sendbird --profile prod --json chat channel create --type group --name 'Tutorial invite' \
  --user-ids '["tutorial_alice"]' --is-distinct false --is-public false
```

Copy `result.channel_url` from the response. In the following commands, replace `<INVITE_CHANNEL_URL>` with it, without the angle brackets.

Invite Bob, then check each member's `state`:

```bash
sendbird --profile prod chat channel members invite --type group --id <INVITE_CHANNEL_URL> \
  --user-ids '["tutorial_bob"]'
sendbird --profile prod --json chat channel members list --type group --id <INVITE_CHANNEL_URL> --limit 20
```

In Bob's entry, check the `state` value. If it's `invited`, accept the invitation as Bob in terminal B. If it's already `joined`, the invitation was accepted automatically, so skip this command.

```bash
sendbird --profile prod chat channel invitation accept --type group --id <INVITE_CHANNEL_URL> --as-user tutorial_bob
```

In terminal B, after Bob is `joined`, send a message as Bob:

```bash
sendbird --profile prod chat message send --type group --id <INVITE_CHANNEL_URL> \
  --message 'Thanks for the invitation' --as-user tutorial_bob
```

The output starts with `✓ Sent message` followed by a message ID.

## Step 2. Leave the private channel

In terminal B, leave the channel as Bob:

```bash
sendbird --profile prod chat channel leave --type group --id <INVITE_CHANNEL_URL> --yes --as-user tutorial_bob
```

The output starts with `✓ Left channel`. In terminal A, list the members again:

```bash
sendbird --profile prod chat channel members list --type group --id <INVITE_CHANNEL_URL> --limit 20
```

Bob isn't in the member list.

## Step 3. Join a public Group channel

Any user can join a public Group channel without an invitation. In terminal A, create one with Alice as its first member:

```bash
sendbird --profile prod --json chat channel create --type group --name 'Tutorial community' \
  --user-ids '["tutorial_alice"]' --is-distinct false --is-public true
```

In the response, `is_public` is `true`. Copy `result.channel_url`. In the following commands, replace `<COMMUNITY_CHANNEL_URL>` with it, without the angle brackets.

In terminal B, join as Bob without an invitation and start watching:

```bash
sendbird --profile prod chat channel join --type group --id <COMMUNITY_CHANNEL_URL> --as-user tutorial_bob
sendbird --profile prod chat channel watch --type group --id <COMMUNITY_CHANNEL_URL> \
  --seconds 120 --max-events 100 --as-user tutorial_bob
```

Wait for the line that starts with `• ready`. If the watch stops before Alice sends her message, run the `watch` command again.

In terminal A, send a message as Alice:

```bash
sendbird --profile prod chat message send --type group --id <COMMUNITY_CHANNEL_URL> \
  --message 'Welcome to the community' --as-user tutorial_alice
```

Bob's watch shows a `message received` line. The message ID at the end of that line matches the ID after `✓ Sent message` in terminal A.

In terminal B, if the watch is still running, stop it with Ctrl+C. Stopping the watch doesn't remove Bob from the channel. Run this command so that Bob leaves the channel:

```bash
sendbird --profile prod chat channel leave --type group --id <COMMUNITY_CHANNEL_URL> --yes --as-user tutorial_bob
```

## Step 4. Clean up

In terminal A, delete the two channels:

```bash
sendbird --profile prod chat channel delete --type group --id <INVITE_CHANNEL_URL> --yes
sendbird --profile prod chat channel delete --type group --id <COMMUNITY_CHANNEL_URL> --yes
```

If you've finished all tutorials, [delete Alice and Bob](/readme/chat.md#delete-the-test-users).
