ankurjha / comnestor
Comnestor broadcasting driver for Laravel
Requires
- php: ^8.1
- illuminate/support: ^10|^11|^12
README
Comnestor Broadcasting Driver is a Laravel broadcasting driver for sending realtime events through Comnestor websocket infrastructure.
With this package, your Laravel app can broadcast events using the standard ShouldBroadcast pattern. The comnestor driver forwards those events to the Comnestor API, and Comnestor delivers them to websocket clients.
Package name: ankurjha/comnestor
Note: The tests badge is currently a placeholder. Once CI is added, replace it with your real workflow status badge.
Table of Contents
- Key Features
- Requirements
- Quick Start
- Step 1: Install via Composer
- Step 2: Run Installer Command
- Step 3: Configure Environment Variables
- Step 4: Verify Broadcasting Configuration
- Step 5: Create a Broadcast Event
- Step 6: Trigger the Event
- Step 7: Listen from JavaScript
- How Realtime Broadcasting Works
- Troubleshooting
- License
Key Features
- Custom Laravel broadcasting driver named
comnestor - Uses native Laravel broadcasting with
ShouldBroadcast - Signed API requests from Laravel to Comnestor
- Easy setup with
php artisan comnestor:install - Works with Laravel broadcasting conventions similar to Pusher, Ably, and Reverb
Requirements
- PHP
^8.1 - Laravel
10,11, or12
Quick Start
composer require ankurjha/comnestor php artisan comnestor:install
Then set these values in .env:
BROADCAST_CONNECTION=comnestor COMNESTOR_BASE_URL=https://www.comnestor.cloud COMNESTOR_APP_KEY=your-app-key COMNESTOR_APP_SECRET=your-app-secret
You can now dispatch Laravel broadcast events normally.
Step 1: Install via Composer
Install the package in your Laravel app:
composer require ankurjha/comnestor
Step 2: Run Installer Command
Run the package installer:
php artisan comnestor:install
This command will:
- Create
app/Services/ComnestorBroadcasting.php - Ensure
config/broadcasting.phpexists - Add a
comnestorconnection in broadcasting config
Step 3: Configure Environment Variables
Add or update these variables in your .env file:
BROADCAST_CONNECTION=comnestor COMNESTOR_BASE_URL=https://www.comnestor.cloud COMNESTOR_APP_KEY=your-app-key COMNESTOR_APP_SECRET=your-app-secret
Variable reference:
BROADCAST_CONNECTION: selects the active Laravel broadcast driverCOMNESTOR_BASE_URL: Comnestor API base URL (usehttpsin production)COMNESTOR_APP_KEY: public identifier for your appCOMNESTOR_APP_SECRET: private secret used to sign API requests
Step 4: Verify Broadcasting Configuration
After installation, config/broadcasting.php should include this connection:
'connections' => [ 'comnestor' => [ 'driver' => 'comnestor', 'base_url' => env('COMNESTOR_BASE_URL'), 'app_key' => env('COMNESTOR_APP_KEY'), 'app_secret' => env('COMNESTOR_APP_SECRET'), ], ],
If you update environment variables, clear cached config:
php artisan optimize:clear
Step 5: Create a Broadcast Event
Create an event implementing ShouldBroadcast.
<?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; class OrderCreated implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels; public function __construct(public array $order) { } public function broadcastOn(): array { return [new Channel('orders')]; } public function broadcastAs(): string { return 'order.created'; } public function broadcastWith(): array { return ['order' => $this->order]; } }
Tip for new Laravel developers:
broadcastOn()defines the channelbroadcastAs()defines the client-facing event namebroadcastWith()defines the payload sent to clients
Step 6: Trigger the Event
Use a route to quickly test event delivery:
use App\Events\OrderCreated; use Illuminate\Support\Facades\Route; Route::get('/broadcast-test', function () { broadcast(new OrderCreated([ 'id' => 101, 'status' => 'created', 'total' => 499.99, ])); return response()->json([ 'message' => 'Broadcast event sent successfully.', ]); });
Open /broadcast-test in your browser or API client to dispatch one event.
Step 7: Listen from JavaScript
Comnestor provides a lightweight JavaScript client that allows frontend applications to listen for realtime events easily.
Include the Comnestor CDN in your page:
<div id="order-events"></div> <script src="https://comnestor.cloud/cdn/comnestor.js"></script> <script> const socket = Comnestor.connect("APP_KEY", { host: "www.comnestor.cloud" }) const channel = socket.subscribe("orders") channel.listen("order.created", (data) => { const div = document.createElement("div") div.className = "p-3 bg-green-100 border rounded" div.innerHTML = ` <b>New Order Received</b><br> Order ID: ${data.order.id}<br> Status: ${data.order.status}<br> Total: ${data.order.total} ` document.getElementById("order-events").prepend(div) }) </script>
How Realtime Broadcasting Works
- Laravel dispatches an event implementing
ShouldBroadcast - Laravel resolves the active driver (
comnestor) - This package signs and sends the payload to Comnestor API
- Comnestor validates the request and emits to websocket channels
- Subscribed clients receive the event in realtime
Troubleshooting
Driver is not recognized
- Confirm package installation completed without errors
- Run
php artisan optimize:clear - Re-run installer:
php artisan comnestor:install
Events are not reaching Comnestor
- Ensure
BROADCAST_CONNECTION=comnestoris set - Verify
COMNESTOR_BASE_URL,COMNESTOR_APP_KEY,COMNESTOR_APP_SECRET - Confirm your Laravel server can access the Comnestor API URL
- Check application logs for
Comnestor broadcast failed
Websocket client does not receive events
- Verify the client subscribed to the same channel name
- Verify event name matches
broadcastAs() - Verify websocket URL and protocol (
ws://orwss://)
Configuration changes are not applied
- Run
php artisan optimize:clear - Restart your queue workers or app runtime if needed
License
This package is open-sourced software licensed under the MIT license.