Skip to content

Commit

Permalink
ingtegrate pusher and queue service for live messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed Dec 7, 2016
1 parent 6219311 commit 9a3aca1
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 11 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"illuminate/contracts": "5.0.* || 5.1.* || 5.2.* || 5.3.*",
"illuminate/support": "5.0.* || 5.1.* || 5.2.* || 5.3.*",
"sebastian-berc/repositories": "^1.0",
"nesbot/carbon": "^1.21"
"nesbot/carbon": "^1.21",
"pusher/pusher-php-server": "^2.6"
},
"require-dev": {
"graham-campbell/testbench": "^3.1",
Expand All @@ -24,7 +25,10 @@
"autoload": {
"psr-4": {
"Nahid\\Talk\\": "src/"
}
},
"files": [
"src/helpers/talk.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
13 changes: 10 additions & 3 deletions config/talk.php
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' => ''
]
]
];
2 changes: 1 addition & 1 deletion src/Facades/Talk.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Talk extends Facade
*/
protected static function getFacadeAccessor()
{
return 'Talk';
return 'talk';
}
}
61 changes: 61 additions & 0 deletions src/Live/Broadcast.php
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);
}

}
45 changes: 45 additions & 0 deletions src/Live/Webcast.php
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);
}
}
16 changes: 14 additions & 2 deletions src/Talk.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@

namespace Nahid\Talk;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Nahid\Talk\Conversations\ConversationRepository;
use Nahid\Talk\Messages\MessageRepository;

use Nahid\Talk\Live\Broadcast;

class Talk
{
use DispatchesJobs;

protected $config;
/**
* The ConversationRepository class instance.
*
Expand All @@ -30,6 +37,8 @@ class Talk
*/
protected $message;

protected $broadcast;

/**
* Currently loggedin user id.
*
Expand All @@ -43,10 +52,12 @@ class Talk
* @param \Nahid\Talk\Conversations\ConversationRepository $conversation
* @param \Nahid\Talk\Messages\MessageRepository $message
*/
public function __construct(ConversationRepository $conversation, MessageRepository $message)
public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
{
$this->config = $config;
$this->conversation = $conversation;
$this->message = $message;
$this->broadcast = $broadcast;
}

/**
Expand Down Expand Up @@ -83,6 +94,7 @@ protected function makeMessage($conversationId, $message)
'is_seen' => 0,
]);

$this->broadcast->transmission($message);
return $message;
}

Expand Down Expand Up @@ -468,7 +480,7 @@ public function getReceiverInfo($conversationId)
$receiver = $conversation->user_one;
}

$userModel = config('talk.user.model');
$userModel = $this->config('talk.user.model');
$user = new $userModel();

return $user->find($receiver);
Expand Down
23 changes: 20 additions & 3 deletions src/TalkServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public function boot()
{
$this->setupConfig();
$this->setupMigrations();
$this->loadViewsFrom(__DIR__.'/views', 'talk');
}
/**
* Register the application services.
*/
public function register()
{
$this->registerBroadcast();
$this->registerTalk();
}
/**
Expand Down Expand Up @@ -54,9 +56,23 @@ protected function setupMigrations()
*/
protected function registerTalk()
{
$this->app->singleton('Talk', function (Container $app) {
return new Talk($app[ConversationRepository::class], $app[MessageRepository::class]);
$this->app->singleton('talk', function (Container $app) {
return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
});

$this->app->alias('talk', Talk::class);
}

/**
* Register Talk class.
*/
protected function registerBroadcast()
{
$this->app->singleton('talk.broadcast', function (Container $app) {
return new Live\Broadcast($app['config']);
});

$this->app->alias('talk.broadcast', Live\Broadcast::class);
}

/**
Expand All @@ -67,7 +83,8 @@ protected function registerTalk()
public function provides()
{
return [
Talk::class,
'talk',
'talk.broadcast'
];
}
}
23 changes: 23 additions & 0 deletions src/helpers/talk.php
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();
}
}

28 changes: 28 additions & 0 deletions src/views/pusherjs.blade.php
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>

0 comments on commit 9a3aca1

Please sign in to comment.