SocketBee provides an easy-to-use API for sending real-time events to your applications. This PHP SDK enables you to interact with the SocketBee service effortlessly.
To use the SocketBee SDK in your PHP project, you can install it via Composer. Run the following command in your project directory:
composer require socketbee/socketbee
Here's a step-by-step guide on how to use the SocketBee PHP SDK:
Ensure the Composer autoloader is included in your script:
require 'vendor/autoload.php';
Create an instance of the SocketBee
class with your application ID, secret, and key. These credentials can be obtained from your SocketBee Dashboard.
$app_id = getenv('APP_ID');
$secret = getenv('APP_SECRET');
$key = getenv('APP_KEY');
$eventSender = new \SocketBee\SocketBee($app_id, $secret, $key);
Use the sendEvent
method to send an event to a specific channel. The method requires the channel name, event name, and event data.
$s = $eventSender->sendEvent('cool_channel', 'new-message', 'very cool stuff!');
The sendEvent
method returns an array containing the status and the body of the response. You can use this information to handle the success or failure of the event.
var_dump($s);
Below is a complete example demonstrating how to use the SocketBee SDK in a PHP script:
<?php
require 'vendor/autoload.php';
$app_id = getenv('APP_ID');
$secret = getenv('APP_SECRET');
$key = getenv('APP_KEY');
$eventSender = new \SocketBee\SocketBee($app_id, $secret, $key);
$s = $eventSender->sendEvent('cool_channel', 'new-message', 'very cool stuff!');
var_dump($s);
?>
The SocketBee
class constructor accepts an optional configuration array. By default, it uses the following settings:
$config = [
'protocol' => 'https',
'host' => 'east-1.socket.socketbee.com',
'port' => 443,
];
If you are an Enterprise user, you can override these settings by passing a custom configuration array when initializing the SocketBee
class:
$config = [
'protocol' => 'https',
'host' => 'your-custom-host.com',
'port' => 8080,
];
$eventSender = new \SocketBee\SocketBee($app_id, $secret, $key, $config);
For detailed API documentation and additional examples, visit the SocketBee PHP SDK Documentation.
This SDK is open-sourced software licensed under the MIT license.
For any questions or issues, please contact SocketBee Support.