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

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

Frontend Configuration

Pass the channel prefix to your frontend:
// React setup
<ChorusProvider
    userId={user.id}
    channelPrefix={user.tenant_id} // Channel prefix
    schema={types}
>
    <App />
</ChorusProvider>
// Vanilla JS setup
const chorus = new ChorusClient({
    userId: user.id,
    channelPrefix: user.tenant_id,
    database: types
});

Creating Prefix Resolvers

Simple Tenant Resolver

<?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


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.