-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ingtegrate pusher and queue service for live messaging
- Loading branch information
Showing
9 changed files
with
208 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
<?php | ||
return [ | ||
'user' => [ | ||
'table' => 'users', | ||
'model' => 'App\User', | ||
'columns' => ['id', 'name'] | ||
'model' => 'App\User' | ||
], | ||
'broadcast' => [ | ||
'enable' => false, | ||
'app_name' => 'your-app-name', | ||
'pusher' => [ | ||
'app_id' => '', | ||
'app_key' => '', | ||
'app_secret' => '' | ||
] | ||
] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,6 @@ class Talk extends Facade | |
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'Talk'; | ||
return 'talk'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Nahid\Talk\Live; | ||
|
||
use Nahid\Talk\Messages\Message; | ||
use Illuminate\Contracts\Config\Repository; | ||
use Pusher; | ||
use Illuminate\Foundation\Bus\DispatchesJobs; | ||
use Nahid\Talk\Live\Webcast; | ||
|
||
class Broadcast | ||
{ | ||
use DispatchesJobs; | ||
|
||
const CONFIG_PATH = 'talk'; | ||
|
||
protected $config; | ||
protected $options = [ | ||
'encrypted' => false | ||
]; | ||
|
||
public $pusher; | ||
|
||
function __construct(Repository $config) | ||
{ | ||
$this->config = $config; | ||
$this->pusher = $this->connectPusher(); | ||
} | ||
|
||
|
||
protected function connectPusher($options = []) | ||
{ | ||
if ($this->getConfig('broadcast.enable')) { | ||
$appId = $this->getConfig('broadcast.pusher.app_id'); | ||
$appKey = $this->getConfig('broadcast.pusher.app_key'); | ||
$appSecret = $this->getConfig('broadcast.pusher.app_secret'); | ||
|
||
$newOptions = array_merge($this->options, $options); | ||
$pusher = new Pusher($appKey, $appSecret, $appId, $newOptions); | ||
return $pusher; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function transmission(Message $message) | ||
{ | ||
if (!$this->pusher) return false; | ||
|
||
$sender = $message->sender->toArray(); | ||
$messageArray = $message->toArray(); | ||
$messageArray['sender'] = $sender; | ||
$this->dispatch(new Webcast($messageArray)); | ||
} | ||
|
||
public function getConfig($name) | ||
{ | ||
return $this->config->get(self::CONFIG_PATH . '.' .$name); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Nahid\Talk\Live; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Nahid\Talk\Live\Broadcast; | ||
|
||
class Webcast implements ShouldQueue | ||
{ | ||
use InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
|
||
protected $message; | ||
protected $broadcast; | ||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct($message) | ||
{ | ||
|
||
$this->message = $message; | ||
|
||
} | ||
|
||
/* | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle(Broadcast $broadcast) | ||
{ | ||
$this->broadcast = $broadcast; | ||
$toUser = ($this->message['sender']['id']==$this->message['conversation']['user_one'])?$this->message['conversation']['user_two']:$this->message['conversation']['user_one']; | ||
|
||
$channelForUser = $this->broadcast->getConfig('broadcast.app_name') . '-user-' . $toUser; | ||
$channelForConversation = $this->broadcast->getConfig('broadcast.app_name') . '-conversation-' . $this->message['conversation_id']; | ||
|
||
$this->broadcast->pusher->trigger([$channelForUser, $channelForConversation], 'talk-send-message', $this->message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: nahid | ||
* Date: 12/7/16 | ||
* Time: 4:58 PM | ||
*/ | ||
|
||
if(!function_exists('talk_live')) { | ||
function talk_live($options) | ||
{ | ||
$talk__appKey = config('talk.broadcast.pusher.app_key'); | ||
$talk__appName= config('talk.broadcast.app_name'); | ||
|
||
$talk__userChannel['name'] = isset($options['user']['id'])? $talk__appName . '-user-' . $options['user']['id']:''; | ||
$talk__conversationChannel['name'] = isset($options['conversation']['id'])? $talk__appName . '-conversation-' . $options['conversation']['id']:''; | ||
$talk__userChannel['callback'] = isset($options['user']['callback'])?$options['user']['callback']:[]; | ||
$talk__conversationChannel['callback'] = isset($options['conversation']['callback'])?$options['conversation']['callback']:[]; | ||
|
||
return view('talk::pusherjs', compact('talk__appKey', 'talk__userChannel', 'talk__conversationChannel'))->render(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script src="https://js.pusher.com/3.2/pusher.min.js"></script> | ||
<script> | ||
// Enable pusher logging - don't include this in production | ||
Pusher.logToConsole = true; | ||
console.log('talk'); | ||
var pusher = new Pusher('{{$talk__appKey}}', { | ||
encrypted: true | ||
}); | ||
@if(!empty($talk__userChannel['name'])) | ||
var userChannel = pusher.subscribe('{{$talk__userChannel['name']}}'); | ||
userChannel.bind('talk-send-message', function(data) { | ||
@foreach($talk__userChannel['callback'] as $callback) | ||
{!! $callback . '(data)' !!} | ||
@endforeach | ||
}); | ||
@endif | ||
@if(!empty($talk__conversationChannel['name'])) | ||
var conversationChannel = pusher.subscribe('{{$talk__conversationChannel['name']}}'); | ||
conversationChannel.bind('talk-send-message', function(data) { | ||
@foreach($talk__conversationChannel['callback'] as $callback) | ||
{!! $callback . '(data)' !!} | ||
@endforeach | ||
}); | ||
@endif | ||
</script> |