michaelyuen

Untitled

Jan 19th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. if (condition) {
  3.     // update new message to table
  4.     $data['chatid'] = $to_id; // use a unique random string
  5.     // broadcast using pusher notication
  6.     // each active browser with pusher script will get the notification
  7.     $pusher->trigger('__subscribed_channel__', '__chat_session__', $data);
  8. }
  9.  
  10. ?>
  11.  
  12. <!-- for user notification -->
  13. <script type="text/javascript">
  14.  
  15.     // Enable pusher logging - don't include this in production
  16.     Pusher.log = function(message) {
  17.         if (window.console && window.console.log) {
  18.             window.console.log(message);
  19.         }
  20.     };
  21.  
  22.     var pusher = new Pusher('__app_secret__');
  23.     var channel = pusher.subscribe('__subscribed_channel__');
  24.     channel.bind('__chat_session__', function(data) {
  25.         // if broadcase notification received
  26.         var element = document.getElementById("new_msg");
  27.         // database was updated with your id
  28.         // if your id is data.chatid goto server and check your messages
  29.         if (data.chatid == '<?= $_SESSION['user']['chatid']; ?>') {
  30.             // use ajax to get new messages so you can authenticate user and only result
  31.             // you can create a alert with html/css
  32.             // using push notication, you don't have to pull message with setInterval
  33.             // if you use pulling, server will have to make query every interval EACH user
  34.             // Hence, if you have 1000 active browser with 1 second interval, you may have 1000 query per second even when no new message is added to server
  35.         } else {
  36.         // do nothing
  37.         }
  38.     });
  39. </script>
Advertisement
Add Comment
Please, Sign In to add comment