Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (condition) {
- // update new message to table
- $data['chatid'] = $to_id; // use a unique random string
- // broadcast using pusher notication
- // each active browser with pusher script will get the notification
- $pusher->trigger('__subscribed_channel__', '__chat_session__', $data);
- }
- ?>
- <!-- for user notification -->
- <script type="text/javascript">
- // Enable pusher logging - don't include this in production
- Pusher.log = function(message) {
- if (window.console && window.console.log) {
- window.console.log(message);
- }
- };
- var pusher = new Pusher('__app_secret__');
- var channel = pusher.subscribe('__subscribed_channel__');
- channel.bind('__chat_session__', function(data) {
- // if broadcase notification received
- var element = document.getElementById("new_msg");
- // database was updated with your id
- // if your id is data.chatid goto server and check your messages
- if (data.chatid == '<?= $_SESSION['user']['chatid']; ?>') {
- // use ajax to get new messages so you can authenticate user and only result
- // you can create a alert with html/css
- // using push notication, you don't have to pull message with setInterval
- // if you use pulling, server will have to make query every interval EACH user
- // 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
- } else {
- // do nothing
- }
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment