Skip to content

Commit f43ec7e

Browse files
committed
Init commit �
0 parents  commit f43ec7e

20 files changed

+1765
-0
lines changed

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) <Maksim Kiselev>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Module.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace mkiselev\broadcasting;
4+
5+
use mkiselev\broadcasting\broadcasters\Broadcaster;
6+
use mkiselev\broadcasting\components\BroadcastManager;
7+
use yii\base\Application;
8+
use yii\base\BootstrapInterface;
9+
use yii\di\Instance;
10+
11+
class Module extends \yii\base\Module implements BootstrapInterface
12+
{
13+
public $defaultRoute = 'broadcasting/auth';
14+
/**
15+
* @var string|array|\mkiselev\broadcasting\components\BroadcastManager
16+
*/
17+
public $broadcastManager = BroadcastManager::class;
18+
19+
/**
20+
* @var string|array|\mkiselev\broadcasting\broadcasters\Broadcaster
21+
*/
22+
public $broadcaster;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public $controllerNamespace = 'mkiselev\broadcasting\controllers';
28+
29+
30+
/**
31+
* @return \mkiselev\broadcasting\components\BroadcastManager|string
32+
*/
33+
public function getBroadcastManagerInstance()
34+
{
35+
if (!is_object($this->broadcastManager)) {
36+
$this->broadcastManager = Instance::ensure($this->broadcastManager, BroadcastManager::class);
37+
}
38+
39+
return $this->broadcastManager;
40+
}
41+
42+
/**
43+
* @return \mkiselev\broadcasting\broadcasters\Broadcaster|string
44+
*/
45+
public function getBroadcasterInstance()
46+
{
47+
if (!is_object($this->broadcaster)) {
48+
$this->broadcaster = Instance::ensure($this->broadcaster, Broadcaster::class);
49+
}
50+
51+
return $this->broadcaster;
52+
}
53+
54+
55+
/**
56+
* Bootstrap method to be called during application bootstrap stage.
57+
* @param Application $app the application currently running
58+
*/
59+
public function bootstrap($app)
60+
{
61+
}
62+
63+
}

README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Yii2-broadcasting
2+
=================
3+
Websocket broadcasting module
4+
5+
[![Latest Stable Version](https://poser.pugx.org/mkiselev/yii2-broadcasting/v/stable.png)](https://packagist.org/packages/mkiselev/yii2-broadcasting)
6+
[![Total Downloads](https://poser.pugx.org/mkiselev/yii2-redis/broadcasting.png)](https://packagist.org/packages/mkiselev/yii2-broadcasting)
7+
8+
This module is made under inspiration of laravel echo and compatible with libraries.
9+
10+
There are several broadcast tools available for your choice:
11+
12+
1) [NullBroadcaster](broadcasters/NullBroadcaster.php) Doing nothing, just a stub
13+
2) [LogBroadcaster](broadcasters/LogBroadcaster.php) Broadcast events to application log
14+
3) [RedisBroadcaster](broadcasters/RedisBroadcaster.php) Broadcast by Redis using Pub/Sub feature (required [yii2-redis](https://github.com/yiisoft/yii2-redis))
15+
4) RatchetBroadcaster (coming soon...)
16+
5) PusherBroadcaster Broadcast by using pusher.com (coming soon...)
17+
18+
19+
Installation
20+
------------
21+
22+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
23+
24+
Either run
25+
26+
```
27+
composer require --prefer-dist mkiselev/yii2-broadcasting "*"
28+
```
29+
30+
or add
31+
32+
```
33+
"mkiselev/yii2-broadcasting": "*"
34+
```
35+
36+
to the require section of your `composer.json` file.
37+
38+
39+
Application configuration
40+
-------------------------
41+
Configure module for use some broadcaster and configure channels auth callbacks:
42+
```php
43+
'bootstrap' => ['broadcasting'],
44+
'modules' => [
45+
'broadcasting' => [
46+
'class' => \mkiselev\broadcasting\Module::class,
47+
'broadcaster' => [
48+
'class' => \mkiselev\broadcasting\broadcasters\RedisBroadcaster::class,
49+
// By default will be used redis application component, but you can configure as you want
50+
'redis' => [
51+
'class' => \yii\redis\Connection::class,
52+
],
53+
// Configure auth callback for private and presitance chanells
54+
'channels' => [
55+
'signal' => function (\yii\web\User $user) {
56+
return $user->can('something');
57+
},
58+
],
59+
],
60+
],
61+
],
62+
```
63+
64+
Configure auth endpoint in urlManager component, by default using 'example.com/broadcasting/auth'
65+
```php
66+
'broadcasting/auth' => '/broadcasting/default/index'
67+
```
68+
69+
70+
Socket.io server configuration
71+
------------------------------
72+
This module is compilable with [laravel-echo-server](https://github.com/tlaverdure/laravel-echo-server)
73+
74+
Please follow to laravel-echo-server instructions to install and run them.
75+
76+
77+
Usage
78+
-----
79+
80+
### Server side
81+
Write your event extended by \mkiselev\broadcasting\events\BroadcastEvent like this:
82+
```php
83+
<?php
84+
85+
namespace common\models;
86+
87+
use mkiselev\broadcasting\channels\PrivateChannel;
88+
use mkiselev\broadcasting\events\BroadcastEvent;
89+
90+
class SignalEvent extends BroadcastEvent
91+
{
92+
public $someParam = 42;
93+
94+
public function broadcastOn()
95+
{
96+
return new PrivateChannel('signal');
97+
}
98+
99+
public function broadcastAs()
100+
{
101+
return 'new';
102+
}
103+
}
104+
```
105+
106+
And broadcast it somewhere:
107+
```php
108+
(new common\models\SignalEvent(['someParam' => 146]))->toOthers()->broadcast();
109+
```
110+
111+
112+
### Client side
113+
Register mkiselev\broadcasting\assets\EchoAsset
114+
import socket.io library https://github.com/tlaverdure/laravel-echo-server#socketio-client-library
115+
116+
```js
117+
window.io = io;
118+
window.Echo = new Echo({
119+
broadcaster: 'socket.io',
120+
host: window.location.hostname + ':6001'
121+
});
122+
123+
Echo.channel('signal')
124+
.listen('.new', function(e) {
125+
console.log(e.someParam);
126+
});
127+
```

assets/EchoAsset.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace mkiselev\broadcasting\assets;
4+
5+
use yii\web\AssetBundle;
6+
7+
class EchoAsset extends AssetBundle
8+
{
9+
public $sourcePath = '@vendor/mkiselev/yii2-broadcasting/assets/echo';
10+
11+
public $js = [
12+
(YII_DEBUG) ? 'js/echo.js' : 'js/echo.min.js',
13+
];
14+
}

assets/echo/LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) <Taylor Otwell>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

0 commit comments

Comments
 (0)