简体   繁体   中英

Push notifications in php

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.

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

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>

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).

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM