| 123456789101112131415161718192021222324252627282930313233 |
- <?php
- namespace App\Services;
- use Illuminate\Support\Facades\Log;
- use ElephantIO\Client;
- class WebSocketService
- {
- public function emitToUser($userId, $event, $data)
- {
- try {
- $url = 'http://localhost:3200';
- $socketClient = new Client(Client::engine(Client::CLIENT_4X, $url));
- $socketClient->initialize();
- $socketClient->of('/');
-
- // Emitir evento con userId para que app.js lo distribuya
- $socketClient->emit('laravel_emit', [
- 'event' => $event,
- 'data' => $data,
- 'userId' => $userId
- ]);
-
- $socketClient->close();
-
- return true;
- } catch (\Exception $e) {
- Log::error('WebSocket emit failed: ' . $e->getMessage());
- return false;
- }
- }
- }
|