Installation
composer require axelbest/sendbird-client
Ensure your project meets the PHP 8.0+ and Symfony 6.0+ requirements.
Configuration
Create a sendbird.yaml in config/packages/ (or merge into existing config):
sendbird:
api_key: 'YOUR_SENDBIRD_API_KEY'
api_secret: 'YOUR_SENDBIRD_API_SECRET'
application_id: 'YOUR_APP_ID'
base_uri: 'https://api.sendbird.com/v3' # Default, optional
First Use Case Initialize the client in a service or controller:
use Axelbest\SendbirdClient\SendbirdClient;
$client = new SendbirdClient();
$users = $client->users()->listUsers();
config/services.yaml:
services:
Axelbest\SendbirdClient\SendbirdClient: '@sendbird.client'
Then autowire it:
public function __construct(private SendbirdClient $sendbird) {}
User Management
// Create user
$user = $sendbird->users()->createUser('user_id', 'nickname', 'profile_url');
// Update user
$sendbird->users()->updateUser('user_id', ['nickname' => 'new_name']);
// List users (paginated)
$users = $sendbird->users()->listUsers(['limit' => 20, 'offset' => 0]);
Channel Operations
// Create group channel
$channel = $sendbird->channels()->createGroupChannel(
'channel_url',
['name' => 'General', 'is_distinct' => true]
);
// Send message
$sendbird->messages()->sendUserMessage(
'channel_url',
'user_id',
'Hello world!'
);
Webhook Handling Use Symfony’s HTTP client to verify webhooks (not wrapped by the package):
$response = $this->httpClient->request('GET', 'https://api.sendbird.com/v3/webhooks/verify', [
'query' => ['timestamp' => $timestamp, 'token' => $token]
]);
Event-Driven Architecture Pair with Symfony’s Messenger component to process Sendbird webhooks asynchronously:
$message = new SendbirdWebhookMessage($payload);
$this->messageBus->dispatch($message);
Caching Responses Cache user/channel lists (TTL: 5–10 minutes) to reduce API calls:
$cacheKey = "sendbird:users:{$offset}";
return $cache->get($cacheKey, fn() => $sendbird->users()->listUsers(['offset' => $offset]));
Error Handling
Wrap calls in try-catch for SendbirdException:
try {
$sendbird->users()->banUser('user_id');
} catch (SendbirdException $e) {
$this->addFlash('error', $e->getMessage());
}
API Key Exposure
api_secret in sendbird.yaml risks exposure.parameter_bag with environment variables:
sendbird:
api_secret: '%env(SENDBIRD_API_SECRET)%'
Rate Limiting
listUsers()) can hit Sendbird’s 1000-requests/minute limit.next cursors or limit results:
$users = $sendbird->users()->listUsers(['limit' => 100]);
Webhook Verification
HttpClient manually:
$isValid = $this->httpClient->request('GET', 'https://api.sendbird.com/v3/webhooks/verify', [
'query' => ['timestamp' => $payload['timestamp'], 'token' => $payload['token']]
])->getStatusCode() === 200;
Deprecated Methods
Enable Debug Mode
Set debug: true in config to log raw API responses:
sendbird:
debug: true
Symfony Profiler
Use the VarDumper component to inspect responses:
$users = $sendbird->users()->listUsers();
dump($users); // Symfony's VarDumper
Custom Responses
Override the Serializer to transform responses:
$client->setSerializer(new CustomSendbirdSerializer());
Middleware
Add HTTP middleware (e.g., retry logic) via Symfony’s HttpClient:
$client->setHttpClient($httpClient->withOptions([
'max_retries' => 3,
'timeout' => 10,
]));
Testing Mock the client in PHPUnit:
$mockClient = $this->createMock(SendbirdClient::class);
$mockClient->method('users')->willReturnSelf();
$mockClient->method('listUsers')->willReturn(['users' => []]);
How can I help you explore Laravel packages today?