A powerful, flexible WebSocket implementation for Laravel applications, enabling real-time communication across various use cases.
- Dynamic channel creation
- Flexible subscription management
- Generic event broadcasting
- User-specific and global messaging
- Comprehensive error handling
- Detailed logging
composer require sentixtech/websocket
php artisan vendor:publish --provider="SentixTech\WebSocket\WebSocketServiceProvider"
Edit config/websocket.php
:
return [
'host' => env('WEBSOCKET_HOST', 'localhost'),
'port' => env('WEBSOCKET_PORT', 8080),
'max_clients' => env('WEBSOCKET_MAX_CLIENTS', 1000),
'max_frame_size' => env('WEBSOCKET_MAX_FRAME_SIZE', 1024 * 1024), // 1MB
];
php artisan websocket:serve
// Create channels
WebSocket::createChannel('notifications');
WebSocket::createChannel('user_events');
// Subscribe a client to a channel
WebSocket::subscribe($socketResource, 'notifications', $userId);
// Unsubscribe from a channel
WebSocket::unsubscribe($socketResource, 'notifications', $userId);
// Broadcast to all channel subscribers
WebSocket::broadcast('notifications', [
'type' => 'alert',
'message' => 'System maintenance in 10 minutes'
]);
// Broadcast to specific users
WebSocket::broadcast('notifications', $message, [
'users' => [1, 2, 3] // Only send to these user IDs
]);
// Emit a generic event
WebSocket::emit('user_login', [
'user_id' => 123,
'timestamp' => now()
]);
// Get number of channel subscribers
$subscriberCount = WebSocket::subscribers('notifications');
// Send a notification to specific users
WebSocket::broadcast('notifications', [
'title' => 'New Message',
'content' => 'You have a new message from John',
'user_ids' => [5, 10] // Only notify these users
]);
// Send a chat message
WebSocket::broadcast('chat_room_1', [
'sender_id' => Auth::id(),
'message' => $messageContent
]);
- Supports user-specific channel subscriptions
- Flexible access control
- Comprehensive error logging
- Non-blocking socket implementation
- Configurable max clients and frame size
- Efficient channel and user management
All methods return boolean or integer status:
createChannel()
: Returnstrue/false
subscribe()
: Returnstrue/false
broadcast()
: Returns number of successful broadcastsemit()
: Returns number of successful event emissions
- Check
storage/logs/laravel.log
for WebSocket errors - Ensure WebSocket server is running
- Verify configuration in
config/websocket.php
- PHP 7.4+
- Laravel 8.0+
- Sockets extension
Contributions are welcome! Please submit pull requests to the repository.
MIT License
For issues and support, please open a GitHub issue in the repository.