Installation
composer require antwebes/api-social-bundle
Ensure Antwebes\ApiSocialBundle\AntwebesApiSocialBundle is enabled in config/bundles.php:
return [
// ...
Antwebes\ApiSocialBundle\AntwebesApiSocialBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console config:dump-reference AntwebesApiSocialBundle
Update config/packages/antwebes_api_social.yaml with your Chatsfree API credentials (client ID, secret, etc.).
First Use Case: Authenticate a User
Inject the ChatsfreeClient service in a controller:
use Antwebes\ApiSocialBundle\Service\ChatsfreeClient;
class ChatController extends AbstractController
{
public function __construct(private ChatsfreeClient $chatsfreeClient) {}
public function login(User $user)
{
$token = $this->chatsfreeClient->authenticate([
'username' => $user->getEmail(),
'password' => $user->getPlainPassword(),
]);
return new JsonResponse(['token' => $token]);
}
}
User Authentication
Use ChatsfreeClient::authenticate() to generate tokens for Chatsfree API calls:
$token = $this->chatsfreeClient->authenticate([
'username' => 'user@example.com',
'password' => 'secure123',
]);
Chat Session Management
Create/join chat rooms via ChatsfreeClient::createChat():
$chatId = $this->chatsfreeClient->createChat([
'title' => 'Support Room',
'participants' => [$user1->getId(), $user2->getId()],
]);
Message Handling
Send/retrieve messages using ChatsfreeClient::sendMessage() and ChatsfreeClient::getMessages():
$this->chatsfreeClient->sendMessage($chatId, 'Hello!');
$messages = $this->chatsfreeClient->getMessages($chatId);
chatsfree.token.refresh events (if supported).ChatsfreeClient in services/controllers.$cache = $this->container->get('cache.app');
$profile = $cache->get('chatsfree_profile_' . $userId, function() use ($userId) {
return $this->chatsfreeClient->getUserProfile($userId);
});
API Rate Limits
try {
$response = $this->chatsfreeClient->createChat($data);
} catch (RateLimitException $e) {
sleep($e->getRetryAfter());
retry();
}
Token Expiry
TokenExpiredException by refreshing:
try {
$this->chatsfreeClient->sendMessage($chatId, 'Hi');
} catch (TokenExpiredException $e) {
$this->chatsfreeClient->refreshToken();
retry();
}
Configuration Overrides
antwebes_api_social.yaml is merged correctly. Use dump-reference to verify:
php bin/console config:dump-reference AntwebesApiSocialBundle
debug: true in config to log raw API responses:
antwebes_api_social:
debug: true
Mockery to stub ChatsfreeClient in unit tests:
$mock = Mockery::mock(ChatsfreeClient::class);
$mock->shouldReceive('authenticate')->andReturn('mock_token');
$this->container->set('antwebes.chatsfree.client', $mock);
Custom API Endpoints
Extend Antwebes\ApiSocialBundle\Service\ChatsfreeClient to add unsupported endpoints:
class CustomChatsfreeClient extends ChatsfreeClient
{
public function customEndpoint(array $data)
{
return $this->request('POST', '/custom', $data);
}
}
Register as a service in services.yaml:
services:
App\Service\CustomChatsfreeClient:
decorates: 'antwebes.chatsfree.client'
Event Dispatching
Dispatch custom events (e.g., chatsfree.message.sent) by extending the bundle’s event system (if not natively supported).
How can I help you explore Laravel packages today?