<?php
// Laravel example using Guzzle HTTP client
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class WebSocketBroadcaster
{
private $client;
private $apiKey;
private $serverUrl;
public function __construct()
{
$this->client = new Client();
$this->apiKey = config('websocket.api_key');
$this->serverUrl = config('websocket.server_url');
}
public function broadcast($tenantId, $message)
{
try {
$response = $this->client->post("{$this->serverUrl}/broadcast", [
'json' => [
'tenantId' => $tenantId,
'message' => $message,
'apiKey' => $this->apiKey
]
]);
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
Log::error('WebSocket broadcast failed: ' . $e->getMessage());
return false;
}
}
}
// Usage
$broadcaster = new WebSocketBroadcaster();
$broadcaster->broadcast('tenant1', 'New notification from Laravel!');
cURL Example
curl -X POST https://websockets.new-cbc.com/broadcast \
-H "Content-Type: application/json" \
-d '{
"tenantId": "tenant1",
"message": "Hello from cURL!",
"apiKey": "your_super_secret_api_key"
}'
๐ง Troubleshooting
Common Issues
Connection Refused / Can't Connect
# Check if the service is running
pm2 status
pm2 logs websockets-app
# Check if port 3000 is accessible
sudo netstat -tlnp | grep :3000
# Restart the service
pm2 restart websockets-app