OddSockets PHP SDK
Official PHP SDK for OddSockets real-time messaging platform
Overview & Features
The OddSockets PHP SDK provides a powerful, event-driven interface for real-time messaging in PHP applications, built on ReactPHP for high-performance async operations.
Modern PHP
Built for PHP 8.1+ with strict typing, union types, and modern language features.
Event-Driven
ReactPHP-based async architecture with EventEmitter pattern and Promise support.
JS Compatible
Identical API to JavaScript SDK for consistent cross-platform development.
High Performance
Optimized WebSocket connections with automatic reconnection and load balancing.
Cost Effective
No per-message pricing, industry-standard 32KB message limits, transparent pricing.
Automatic Failover
Built-in redundancy and intelligent error handling for 99.9% uptime.
Installation
composer require oddsockets/php-sdk
{
"require": {
"php": "^8.1",
"ext-json": "*",
"ext-curl": "*",
"guzzlehttp/guzzle": "^7.8",
"ratchet/pawl": "^0.4",
"react/promise": "^3.0",
"evenement/evenement": "^3.0"
}
}
Quick Start
Basic Usage
<?php
require_once 'vendor/autoload.php';
use OddSockets\OddSockets;
use React\EventLoop\Loop;
// Create client
$client = OddSockets::create('ak_live_1234567890abcdef');
// Get channel
$channel = $client->channel('my-channel');
// Subscribe to messages
$channel->subscribe(function ($message) {
echo "Received: " . json_encode($message) . "\n";
});
// Publish a message
$channel->publish('Hello, World!');
// Start the event loop
Loop::get()->run();
Advanced Configuration
<?php
use OddSockets\Config\OddSocketsConfig;
$config = OddSocketsConfig::builder('ak_live_1234567890abcdef')
->userId('user123')
->autoConnect(false)
->reconnectAttempts(3)
->timeout(15)
->build();
$client = OddSockets::create($config);
$client->on('connected', function () {
echo "Connected to OddSockets!\n";
});
$client->connect();
Promise-Based Operations
<?php
$channel = $client->channel('async-channel');
$channel->subscribe($callback)
->then(function () {
echo "Successfully subscribed\n";
return $channel->publish(['message' => 'Hello Async!']);
})
->then(function ($result) {
echo "Message published: " . json_encode($result) . "\n";
})
->otherwise(function ($error) {
echo "Error: " . $error->getMessage() . "\n";
});
Configuration
Client Options
$client = OddSockets::create([
'apiKey' => 'your-api-key', // Required: Your OddSockets API key
'userId' => 'user-id', // Optional: User identifier
'autoConnect' => true, // Optional: Auto-connect on creation
'reconnectAttempts' => 5, // Optional: Max reconnection attempts
'heartbeatInterval' => 30000 // Optional: Heartbeat interval (ms)
]);
Channel Options
$channel->subscribe($callback, [
'enablePresence' => true, // Enable presence tracking
'retainHistory' => true, // Retain message history
'maxHistory' => 100 // Maximum history size
]);
$channel->publish($message, [
'ttl' => 3600, // Time to live (seconds)
'metadata' => ['priority' => 'high'], // Additional metadata
'storeInHistory' => true // Store in message history
]);
Examples
Explore comprehensive examples demonstrating the OddSockets PHP SDK in action:
Performance & Compatibility
OddSockets PHP SDK delivers superior performance with broad compatibility:
PHP Support
- PHP 8.1+ (Latest)
- ReactPHP Ecosystem
- Composer Package
- PSR-4 Autoloading
Framework Support
- Laravel Integration
- Symfony Compatible
- Standalone Usage
- ReactPHP Apps
Framework Integrations
The OddSockets PHP SDK works seamlessly with popular PHP frameworks. Here are examples showing how to integrate:
Laravel
<?php
namespace App\Services;
use OddSockets\OddSockets;
use React\EventLoop\Loop;
class OddSocketsService
{
private $client;
public function __construct()
{
$this->client = OddSockets::create(config('oddsockets.api_key'));
$this->client->on('connected', function () {
\Log::info('OddSockets connected');
});
}
public function publishToChannel(string $channel, $message)
{
$channel = $this->client->channel($channel);
return $channel->publish($message)
->then(function ($result) {
\Log::info('Message published', $result);
return $result;
})
->otherwise(function ($error) {
\Log::error('Publish failed', ['error' => $error->getMessage()]);
throw $error;
});
}
public function startEventLoop()
{
Loop::get()->run();
}
}
Symfony
<?php
namespace App\Service;
use OddSockets\OddSockets;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
class OddSocketsService
{
private $client;
private LoggerInterface $logger;
public function __construct(
#[Autowire('%env(ODDSOCKETS_API_KEY)%')] string $apiKey,
LoggerInterface $logger
) {
$this->logger = $logger;
$this->client = OddSockets::create($apiKey);
$this->client->on('connected', function () {
$this->logger->info('OddSockets connected');
});
$this->client->on('error', function ($error) {
$this->logger->error('OddSockets error', ['error' => $error->getMessage()]);
});
}
public function subscribe(string $channelName, callable $callback): void
{
$channel = $this->client->channel($channelName);
$channel->subscribe($callback)
->then(function () use ($channelName) {
$this->logger->info('Subscribed to channel', ['channel' => $channelName]);
})
->otherwise(function ($error) use ($channelName) {
$this->logger->error('Subscription failed', [
'channel' => $channelName,
'error' => $error->getMessage()
]);
});
}
}
ReactPHP Application
<?php
require_once 'vendor/autoload.php';
use OddSockets\OddSockets;
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;
$loop = Loop::get();
// Create OddSockets client
$oddSockets = OddSockets::create('ak_live_1234567890abcdef', $loop);
// Create HTTP server
$server = new HttpServer($loop, function ($request) use ($oddSockets) {
$channel = $oddSockets->channel('web-events');
// Publish web request event
$channel->publish([
'type' => 'http_request',
'method' => $request->getMethod(),
'uri' => $request->getUri()->getPath(),
'timestamp' => time()
]);
return new Response(200, [], 'Hello from ReactPHP + OddSockets!');
});
// Subscribe to events
$eventChannel = $oddSockets->channel('system-events');
$eventChannel->subscribe(function ($message) {
echo "System event: " . json_encode($message) . "\n";
});
// Start servers
$socket = new SocketServer('127.0.0.1:8080', [], $loop);
$server->listen($socket);
echo "Server running on http://127.0.0.1:8080\n";
echo "OddSockets integration active\n";
$loop->run();