> ## Documentation Index
> Fetch the complete documentation index at: https://chorus.pixelsprout.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Channel Prefixes

> Implement multi-tenant channel routing and custom prefix resolvers for scalable Laravel Chorus applications.

# Channel Prefixes

Channel prefixes enable **multi-tenant architecture** in Laravel Chorus by isolating WebSocket channels and data streams between different organizational units, teams, or tenants.

## What are Channel Prefixes?

Channel prefixes modify WebSocket channel names to create isolated data streams:

```
Without prefix: chorus.user.123
With prefix:    chorus.tenant-456.user.123
```

This ensures users only receive data relevant to their context and provides complete data isolation between tenants.

## Basic Configuration

### Enable Channel Prefixes

Configure a prefix resolver in `config/chorus.php`:

```php theme={null}
<?php

return [
    // ... other config ...
    
    'channel_prefix_resolver' => App\Chorus\Resolvers\TenantPrefixResolver::class,
];
```

### Frontend Configuration

Pass the channel prefix to your frontend:

```tsx theme={null}
// React setup
<ChorusProvider
    userId={user.id}
    channelPrefix={user.tenant_id} // Channel prefix
>
    <App />
</ChorusProvider>
```

```javascript theme={null}
// Vanilla JS setup
const chorus = new ChorusClient({
    userId: user.id,
    channelPrefix: user.tenant_id,
    database: types
});
```

## Creating Prefix Resolvers

### Simple Tenant Resolver

```php theme={null}
<?php

namespace App\Chorus\Resolvers;

use Pixelsprout\LaravelChorus\Contracts\PrefixResolver;

class TenantPrefixResolver implements PrefixResolver
{
    public function resolve($model = null): ?string
    {
        // Get tenant from authenticated user
        $user = auth()->user();
        
        if (!$user || !$user->tenant_id) {
            return null;
        }
        
        return "tenant-{$user->tenant_id}";
    }
}
```

## Channel Structure Examples

### Basic Tenant Isolation

```
Without tenant isolation:
- chorus.user.123
- chorus.table.messages
- chorus.record.messages.456

With tenant isolation:
- chorus.tenant-abc.user.123
- chorus.tenant-abc.table.messages  
- chorus.tenant-abc.record.messages.456
```

### Project-Based Channels

```
Project-specific isolation:
- chorus.project-alpha.user.123
- chorus.project-alpha.table.tasks
- chorus.project-alpha.record.tasks.789
```

## Next Steps

<CardGroup cols={1}>
  <Card title="Getting started" href="/getting-started" icon="bolt">
    Optimize Chorus for large-scale applications
  </Card>
</CardGroup>

***

Channel prefixes are essential for building scalable, multi-tenant applications with Laravel Chorus. Proper implementation ensures data isolation, security, and optimal performance across different organizational contexts.
