So this time round I wanted to experiment with PHP so that I can implement the front end for my little messaging application.
Why PHP? Because it's something that I already know and zeroMQ supports it :)
On the same server I have had to install a few things to get it working.
1] phpize5 - check that you have it available (you could also use phpinfo())
# phpize5 --help
Usage: /usr/bin/phpize5 [--clean|--help|--version|-v]
2] if not then you probably don't have php5-dev
# apt-get install php5-dev
3] clone git repo for php-zmq
# git clone https://github.com/mkoppanen/php-zmq.git
# git clone https://github.com/mkoppanen/php-zmq.git
# cd php-zmq.git
4] then follow the guide to finish it off ...
# phpize
# ./configure
# make && make install
# make test
5] add extension=zmq.so to php.ini
6] lastly
# apache2ctl graceful
So below is my PHP code. There's nothing special about it.
- Firstly I create my context
- subscribe to the socket on port 5556 since this is the port that the sender server sends messages out on
- created a small loop of 100 and each time reading from the socket
<html>
<body>
<?php
$context = new ZMQContext();
// Socket to talk to server
$subscriber = new ZMQSocket($context, ZMQ::SOCKET_SUB);
$subscriber->connect("tcp://localhost:5556");
$subscriber->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
$i = 0;
while( $i < 100 ) {
$string = $subscriber->recv();
echo $i."-".$string."<br>";
$i++;
}
?>
</body>
</html>
<snip>
...
10- docho: dog:1363660548
11- docho: dog:1363660549
12- docho: dog:1363660550
13- docho: dog:1363660551
14- docho: dog:1363660552
15- docho: dog:1363660553
...
</snip>
Challenge
Right now I am creating a random loop to read messages from the queue.
What I need to do is let the web front know when there is a message to display.
I have a couple of ideas on how to do this ...
...
10- docho: dog:1363660548
11- docho: dog:1363660549
12- docho: dog:1363660550
13- docho: dog:1363660551
14- docho: dog:1363660552
15- docho: dog:1363660553
...
</snip>
Challenge
Right now I am creating a random loop to read messages from the queue.
What I need to do is let the web front know when there is a message to display.
I have a couple of ideas on how to do this ...
- memcached - submit every message in memory and let the client loop. But doesn't solve the trigger problem
- add the messages to database and let the client look for updates rows. Not so efficient.
- HTML5 webworks? The Web Workers specification defines an API for spawning background scripts in your web application. Web Workers allow you to do things like fire up long-running scripts to handle computationally intensive tasks, but without blocking the UI or other scripts to handle user interactions. They're going to help put and end to that nasty 'unresponsive script' dialog that we've all come to love. So the worker will connect to the port and update the div that contains the message holder. Thoughts? http://www.html5rocks.com/en/tutorials/workers/basics/



