-
Notifications
You must be signed in to change notification settings - Fork 38
Pusher integration
Buba Suma edited this page Jun 11, 2016
·
2 revisions
Integrating Pusher with simplechat
is very easy.
Create an account, and make a note of your app_id
, app_key
and app_secret
.
In the example below we trigger an event named new_message
to Pusher on a channel called simplechat
with the senderId and receiverId of the message.
namespace common\models;
//...
use Pusher;
class Message extends \bubasuma\simplechat\db\Message
{
/**
* @inheritDoc
*/
public static function create($userId, $contactId, $text)
{
$errors = parent::create($userId, $contactId, $text);
if ( empty($errors) ) {
$options = [
'cluster' => 'APP_CLUSTER',
'encrypted' => true
];
$pusher = new Pusher(
'APP_KEY',
'APP_SECRET',
'APP_ID',
$options
);
$pusher->trigger(
'simplechat',
'new_message',
['senderId' => $userId, 'receiverId' => $contactId]
);
return [];
}
return $errors;
}
}
Include the pusher-js script tag on your page.
<script src="//js.pusher.com/3.1/pusher.min.js"></script>
Now you can define callback that bind to new_message
event on simplechat
channel, coming in via the connection to Pusher:
var $messenger = $('#messenger');
var $conversations = $('#conversations');
var pusher = new Pusher('APP_KEY', {
cluster: 'APP_CLUSTER',
encrypted: true
});
var channel = pusher.subscribe('simplechat');
channel.bind('new_message', function(data) {
if ($conversations.yiiSimpleChatConversations('widget').user.id == data.receiverId) {
$conversations.yiiSimpleChatConversations('load', 'new');
if ($conversations.yiiSimpleChatConversations('widget').current.contact.id == data.senderId) {
$messenger.yiiSimpleChatMessages('load', 'new');
}
}
});