how can I detect notifications on ratchet codeigniter?

75 views
Skip to first unread message

jsd...@gmail.com

unread,
Apr 21, 2017, 9:38:57 PM4/21/17
to Ratchet
public function pusher(){
$data = $this->session->userdata('log');
$user_id = $data['id'];
$firstCall = false;
        if (isset($_GET['timestamp'])) {
            $last_ajax_call = $_GET['timestamp'];
        } else {
            $last_ajax_call = time();
            $firstCall = true;
        }

        clearstatcache();
        $notificationsCount = $this->notification->checkForNotifications($user_id, $last_ajax_call);
        $newData = (int) $notificationsCount > 0 ? true : false;
        $notifications = [];

        if ($newData) {
        $dataSet = $this->notification->getNotifications($user_id, $last_ajax_call);
        foreach ($dataSet as $value ) {
        $notifications[] = $value;
        $finalNotificationTime = $value['timestamp'];
        }

        $result = array('notifications' => $notifications, 'timestamp' => $finalNotificationTime);

        $json = $this->json($result);

        echo $json;
        }else{
        if ($firstCall) {
        $dataSet = $this->notification->getUnreadNotifications($user_id);
        foreach ($dataSet as $value) {
        $notification[] = $value;
        }

        $result = array('notifications' => $notifications, 'timestamp' => $last_ajax_call);

        $json = $this->json($result);

        echo $json;
        }
        }
        
        $context = new ZMQContext();
   $socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
   $socket->connect("tcp://localhost:5555");
 
   $socket->send(json_encode($notifications));
   print_r($notifications);
}



    var sock = new ab.Session('ws://192.168.0.4:8080',
        function() {
            sock.subscribe('kittensCategory', function(topic, data) {
                $badge.show();
                $badge.text(parseInt($badge.text()) + 1);

                $list.find(".item").eq(13).nextAll(".item").remove();
                var item = '<li class="item text-warning"><a href="#"><span class="text-warning">' +
                    '<i class="fa fa-exclamation-triangle fa-fw"></i>'+data.title+' is low'+'</span>' +
                    '<span class="pull-right text-muted small" data-time="">'+data.timestamp+'</span></a></li>' +
                    '<li class="item divider"></li>';
                $list.prepend(item);
                $('.dropdown.open .dropdown-toggle').dropdown('toggle');
            });
        },
        function() {
            console.warn('WebSocket connection closed');
        }, {
            'skipSubprotocolCheck': true
        }
    );



<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
 
class Pusher implements WampServerInterface {
     
    /**
     * A lookup of all the topics clients have subscribed to
     */
    protected $subscribedTopics = array();
     
    public function onSubscribe(ConnectionInterface $conn, $topic) {
        $this->subscribedTopics[$topic->getId()] = $topic;
    }
     
    /**
     * @param string JSON'ified string we'll receive from ZeroMQ
     */
    public function onBlogEntry($entry) {
        $entryData = json_decode($entry, true);
     
        // If the lookup topic object isn't set there is no one to publish to
        if (!array_key_exists($entryData['category'], $this->subscribedTopics)) {
            return;
        }
     
        $topic = $this->subscribedTopics[$entryData['category']];
     
        // re-send the data to all the clients subscribed to that category
        $topic->broadcast($entryData);
    }
     
    /* The rest of our methods were as they were, omitted from docs to save space */
     
    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
     
    }
     
    public function onOpen(ConnectionInterface $conn) {
     
    }
     
    public function onClose(ConnectionInterface $conn) {
     
    }
     
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    }
     
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->close();
    }
     
    public function onError(ConnectionInterface $conn, \Exception $e) {
    }
}
Reply all
Reply to author
Forward
0 new messages