8

I am developing an appointments center web application using php+mysql. what I currently trying to do is to send notification when an appointment has been made from web server to client/user bowers without third party pushers and without using jQuery SetInterval AJAX request. I think SetInterval & AJAX is a bad approach because there would be too much traffic between the client and the server.

How can I implement notifications without polling the server with SetInterval?

Mifeet
  • 12,949
  • 5
  • 60
  • 108
Hadi.M
  • 544
  • 1
  • 6
  • 19
  • You can read my answer to a similar question [here](http://stackoverflow.com/questions/16524727/how-to-output-in-realtime-when-number-is-change/16531635#16531635). I hope it helps. Oh and you won't need to have a node server or anything. – SergioMSCosta May 31 '13 at 23:48

3 Answers3

14

You can do this using NodeJs. NodeJS is javascript on your server that pushes content to connected clients in real time.

Its really easy to use and setup. You need a server dedicated to the real time app, I use http://nodejitsu.com.

Server Side

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

app.listen(8080);

function handler (req, res) {
// parse URL
var requestURL = url.parse(req.url, true);

// if there is a message, send it
if(requestURL.query.message)
    sendMessage(decodeURI(requestURL.query.message));

// end the response
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("");
}

function sendMessage(message) {
io.sockets.emit('notification', {'message': message});
}

Client Side

<script src="socket.io.min.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('notification', function (data) {
    console.log(data.message);
});
</script>

I added the easy to use example by @intivev below to complete the answer for future readers

MDDY
  • 215
  • 3
  • 10
  • 1
    @MDDY I am just at the beginning of this. Please help on this. From where i can get `socket.io.min.js` file? can you provide some more detail on given answer. – Mann Verma Mar 06 '17 at 11:41
5

You can use WebSockets for this, the Ratchet is a PHP implementation built on top of the React library. I've used both these libraries in production applications and have been more than happy with them.

Yes, using node.js is an option and maybe a better one - dependant on your current situation.

Anthony.

Anthony Sterling
  • 2,451
  • 16
  • 10
2

I feel u are choosing the wrong language for this purpose (PHP), there may be a way out to do it in PHP , but i am damp sure that its would be a twisted one. I recommend using node.js for this purpose as using it you can accomplish pushing notifications to client in much simpler way

Server Side

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

app.listen(8080);

function handler (req, res) {
// parse URL
var requestURL = url.parse(req.url, true);

// if there is a message, send it
if(requestURL.query.message)
    sendMessage(decodeURI(requestURL.query.message));

// end the response
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("");
}

function sendMessage(message) {
io.sockets.emit('notification', {'message': message});
}

Client Side

<script src="socket.io.min.js"></script>
<script>
var socket = io.connect('http://localhost:8080');
socket.on('notification', function (data) {
    console.log(data.message);
});
</script>

So you see its lot eassy if you use the suitable language for it

intivev
  • 43
  • 1
  • 5