spiral / broadcast
RoadRunner broadcast plugin bridge
Installs: 29 284
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=7.2
- ext-json: *
- spiral/roadrunner: ^1.1
Requires (Dev)
- phpunit/phpunit: ~8.0
- spiral/code-style: ^1.0
README
This repository contains the codebase bridge for broadcast RoadRunner plugin.
Installation
To install application server and broadcast codebase
$ composer require spiral/roadrunner-broadcast
You can use the convenient installer to download the latest available compatible version of RoadRunner assembly:
$ composer require spiral/roadrunner-cli --dev $ vendor/bin/rr get
Usage
For example, such a configuration would be quite feasible to run:
rpc: listen: tcp://127.0.0.1:6001 server: # Don't forget to create a "worker.php" file command: "php worker.php" relay: "pipes" http: address: 127.0.0.1:80 # Indicate that HTTP support ws protocol middleware: [ "websockets" ] websockets: broker: default path: "/ws" broadcast: default: driver: memory
Read more about all available brokers on the documentation page.
After configuring and starting the RoadRunner server, the corresponding API will become available to you.
<?php use Spiral\Goridge\RPC\RPC; use Spiral\RoadRunner\Broadcast\Broadcast; require __DIR__ . '/vendor/autoload.php'; $broadcast = new Broadcast(RPC::create('tcp://127.0.0.1:6001')); if (!$broadcast->isAvailable()) { throw new \LogicException('The [broadcast] plugin not available'); } // // Now we can send a message to a specific topic // $broadcast->publish('channel-1', 'message for channel #1');
Select Specific Topic
Alternatively, you can also use a specific topic (or set of topics) as a separate entity and post directly to it.
// Now we can select the topic we need to work only with it $topic = $broadcast->join(['channel-1', 'channel-2']); // And send messages there $topic->publish('message'); $topic->publish(['another message', 'third message']);
Read more about all the possibilities in the documentation page.
Client
In addition to the server (PHP) part, the client part is also present in most projects. In most cases, this is a browser in which the connection to the server is made using the WebSocket protocol.
const ws = new WebSocket('ws://127.0.0.1/broadcast'); ws.onopen = e => { const message = { command: 'join', topics: ['channel-1', 'channel-2'] }; ws.send(JSON.stringify(message)); }; ws.onmessage = e => { const message = JSON.parse(e.data); console.log(`${message.topic}: ${message.payload}`); }
Examples
Examples are available in the corresponding directory ./example.
License
The MIT License (MIT). Please see LICENSE
for more information.
Maintained by Spiral Scout.