1

How to implement push notifications in PHP based webapp. What I actually want to implement is to get a notification as soon as some user joins a chat.

suppose two users are participants in a chat, if a third user joins that chat, then other two user be notified.

3
  • You havent really given anything to go on try reading en.wikipedia.org/wiki/Push_technology then search for php and the chosen methodology Commented May 1, 2013 at 12:18
  • I have searched the web like anything, polling is not the thing for me as it is make other request super slow, APE is one more ajax push engine, but again its complicated to setup. I just need quite simple and less on resources. Commented May 1, 2013 at 12:29
  • How are you currently sending messages to the users when someone enters something in the chat box, cant you use the same method. Commented May 1, 2013 at 12:37

5 Answers 5

1

I think you mean notifications to the user using Javascript. This could be done using websockets (HTML5) or javascript that every x seconds makes a call to your server.

Websockets are hard to setup are my experience.

every x seconds is, 1 a waste of resource often and leads to a lot of trouble when you get a lot of visitors.

The solution I use is an api called pusher. I use it only for apps for my self so I use the free version. But it works like a charm and is easy to setup

Also look at http://en.wikipedia.org/wiki/Push_technology, from Anigel

Sign up to request clarification or add additional context in comments.

Comments

1

What about adding NodeJS + SocketIO to your stack?

SocketIO let's you create a room for your clients and it can emit messages to a room of clients.

Let me show you a basic example:

notification.js

var app = require('http').createServer(), 
    io = require('socket.io').listen(app);

app.listen(3000);

io.sockets.on('connection', function (socket) {
    // Joining a room
    socket.join('chatroom');

    socket.on('newUserJoinRequest', function(data) {
        socket.broadcast.to('chatroom').emit('newUserJoinedNotification', data);
    })

});

client.html

<!DOCTYPE html>
<head>   
    <meta charset="utf-8">    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>    
    <script type="text/javascript" src="http://localhost:3000/socket.io/socket.io.js"></script>
</head>
<body>
    <input type="text"  id="username" placeholder="name">
    <button type="text" id="joinButton">Join</button>
    <div id="responses">   
        <h2>NodeJS+SocketIO responses:</h2>   
        <ul></ul>
    </div>

    <script>
        var socket;
        jQuery(document).ready(function() {
            socket = io.connect('//localhost:3000');

            socket.on('newUserJoinedNotification', function(data) {
                var li = '<li>A new user joined the room: ' + data.name + '</li>';    
                jQuery('#responses ul').append(li);
            });
        });

        jQuery('#joinButton').click(function() {
            var username = jQuery('#username').val();
            socket.emit('newUserJoinRequest', {name: username});
        });
    </script> 

</body>
</html>

Comments

0

True push from server to client is not (easily) possible with PHP. What you will need is polling. You can create a message queue on the server for each connected client and the client automatically retrieves the contents of that queue with a poller (so basically doing requests every set interval).

1 Comment

this seems most optimal solution but still I would prefer to have a push engine that is quite simple to setup.
0

If you want it to be simple, then you need to look into either polling, long polling or using an api. http://en.wikipedia.org/wiki/Push_technology

There is no way using php to make the server open up a connection via proxies, firewalls etc to a program on a computer when that computer hasn't already got a connection to the server and said it is waiting for data.

That is effectively what long polling does, the client makes a connection to the server and keeps it open waiting for the server to tell it something, or until it times out. If it times out, then it reconnects

Comments

0

You can set up push notifications without developing your own app using notify.pm. It takes around 2 lines of code, the API is here: https://github.com/notifypm/notifypm-php-api

<?php

require_once("notifypm.class.php");

$n = new Notifypm('email', 'password', '.pmNumber');
$n->push('Message!', 'Link (optional)!');

$n->disconnect();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.