own3d / socket
OWN3D Socket Client
Installs: 2 052
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^6.3|^7.0
- illuminate/support: ~5.4|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- own3d/id: ^1.7|^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18
- orchestra/testbench: ^6.14
- own3d/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^9.5
README
Installation
composer require own3d/socket
Configuration
Add the own3d-socket
configuration within the config/services.php
config file.
'own3d-socket' => [ 'username' => env('OWN3D_SOCKET_USERNAME', 'own3d-socket'), 'password' => env('OWN3D_SOCKET_PASSWORD') 'secret' => env('OWN3D_SOCKET_SECRET'), ],
Configure as Laravel Broadcaster
OWN3D Socket can be also used as Laravel Broadcaster. Important is, all events MUST prefixed with your own3d/id client id. This will be already done in the broadcaster and can be configured with the room_prefix
.
Add the own3d-socket
configuration within the config/broadcasting.php
config file.
'own3d-socket' => [ 'driver' => 'own3d-socket', 'room_prefix' => env('OWN3D_ID_KEY'), ],
On the client side, use:
import {io} from 'socket.io-client'; const socket = io('https://socket-hel1-1.own3d.dev', { withCredentials: true, }); socket .on('connect', () => { console.log('Socket connected.', socket.id); socket.emit('room', '<room-prefix>.<channel-name>'); }) .on('App\\Events\\ExampleEvent', (e) => { // e contains additional event data });
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 ExampleEvent implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels; private string $userId; /** * Get the channels the event should broadcast on. * * @return Channel|array */ public function broadcastOn() { return new Channel('channel-name'); } }