Sunday, December 21, 2014

Introduction to Server-Sent Events with PHP example

Server-sent events (SSE) is a web technology where a browser receives automatic updates from a server via HTTP protocol. SSE was known before as EventSource and first is introduced in 2006 by Opera. During 2009 W3C started to work on first draft. And here is a latest W3C proposed recommendation from December 2014. It is little know feature that was implemented by all major web browsers except Internet Explorer and may be this is the reason why it is not widely known and used. The idea behind Server-sent events is very simple – a web application subscribes to a stream of updates generated by a server and, whenever a new event occurs, a notification is sent to the client.
But to really understand power of Server-Sent Events, we need to understand the limitations of AJAX version. First was Polling – polling is a technique used by majority of AJAX applications. Idea is that the JavaScript via AJAX repeatedly polls a server for a new data in a given interval (5 seconds for example). If there is new data, server returns it. If there is no new data, server simply return nothing. The problem with this technique is that creates additional overhead. Each time connection needs to be open and then closed.
Next method that was introduces was Long polling (aka COMET). Difference between polling and long polling is that when request is made and there is no data – server simply hangs until new data comes. Then server returns data and closes connection. This was also know as hanging GET method. So instead to returns empty response server waits until data comes, then returns data and closes HTTP connection.
Next comes WebSocket which is bi-directional rich media protocol and can be used in a lot of cases. But WebSocket needs a different protocol, different server side code and it is a little bit complicated compared to SSE.
So what is good for Server-sent events? It could be used in cases when data communicates in one way – from server to client. Here are couple of cases which SSE is very useful: real-time stock prices update; live score and sports events; server monitoring web applications.
Benefit of using Server-sent events instead of AJAX polling or long polling is that technology is directly supported by major web browsers; protocol that is used is HTTP so it is very easy to implement it on server side as well. SSE does not generate overhead and everything that you need is handled by web browser. Here is current state of SSE support.

Protocol description

Data are sent in plain text. So SSE is not suitable for binary data, but it is perfect for text events. First step is to set correct response header Content-Type to text/event-stream.
header("Content-Type: text/event-stream");
Next step is to construct and send data. Basically response contains keyword data followed by data you want to send and two new lines.
data: Hello World!\n\n
If you want to send multiple lines you can separate them by one new line.
data: Hello World!\n
data: This is a second line!\n\n
Here is how to send JSON data:
data: {"msg": "Hello World!"}\n\n
Or to split JSON in multiple lines:
data: {\n
data: "msg": "Hello World!",\n
data: "line2": "This is a second line!"\n
data: }\n\n
Here is PHP code that does the above:
header("Content-Type: text/event-stream");
echo "data: Hello World!\n\n";
This is one event with in multiple lines. Note that new line character is a separator. if you need to send new line character consider to escape it or to properly split message in multiple lines:
header("Content-Type: text/event-stream");
echo "data: Hello World!\n";
echo "data: This is a second line!\n\n";
Here is how to send to events with some delay:
header("Content-Type: text/event-stream");
echo "data: First message\n\n";

echo "data: Second message\n\n";
But what happens when connection is lost or closed – well browser opens again connection after 3 seconds. To control re-connection time you should use a keyword retry with first message. Number passed after retry is in milliseconds. Here is example – tell browser to reconnect after 2 seconds if HTTP connection is lost.
header("Content-Type: text/event-stream");
echo "retry: 2000\n";
echo "data: Hello World!\n\n";
When browser reconnects meanwhile some event happens how do you know which was sent and which was not – well you can associate unique id with each event. When reconnects browser send HTTP header Last-Event-ID. Based on that header you know which is the last event browser received.
id: 1\n
data: Hello World!\n\n

id: 2\n
data: Second message\n\n

JavaScript API

Using EventSource in browser is simple and easy. First you check that browser supports EventSource API then you create event source by passing URL to which to listen.
if (!!window.EventSource) {
    var source = new EventSource("data.php");
} else {
    alert("Your browser does not support Server-sent events! Please upgrade it!");
EventSource object has three listeners to subscribe. Most important is message, others are open and error.
source.addEventListener("message", function(e) {
}, false);

source.addEventListener("open", function(e) {
    console.log("Connection was opened.");
}, false);

source.addEventListener("error", function(e) {
    console.log("Error - connection was lost.");
}, false);
Most important properties of Event object passed to listener functions are data and lastEventId.
One interesting feature is named events. You can specify name of different events and on client side different listeners to be fired based on that events.
event: priceUp\n
data: GOOG:540\n\n
Then on client side you can subscribe to this event by passing it to listener function:
source.addEventListener("priceUp", function(e) {
    console.log("Price UP - " +;
}, false);

PHP Server Code

Only change in PHP code (or other server side code) is that you need of infinite loop to keep connection open. Here is some code from example:
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");

if (isset($lastId) && !empty($lastId) && is_numeric($lastId)) {
    $lastId = intval($lastId);

while (true) {
    $data = \\ query DB or any other source - consider $lastId to avoid sending same data twice
    if ($data) {
        sendMessage($lastId, $data);

function sendMessage($id, $data) {
    echo "id: $id\n";
    echo "data: $data\n\n";

Show me example

After so many words and code snippets I put all together and created simple Stock Tickets web application that updates price of some selected stocks. Data source is not real but it is simple multidimensional array with very simple structure – ticked – price. Otherwise everything is real and could be used to explore and study the code.

What is good audio editing software on Linux

Whether you are an amateur musician or just a student recording his professor, you need to edit and work with audio recordings. If for a long time such task was exclusively attributed to Macintosh, this time is over, and Linux now has what it takes to do the job. In short, here is a non-exhaustive list of good audio editing software, fit for different tasks and needs.

1. Audacity

Let's get started head on with my personal favorite. Audacity works on Windows, Mac, and Linux. It is open source. It is easy to use. You get it: Audacity is almost perfect. This program lets you manipulate the audio waveform from a clean interface. In short, you can overlay tracks, cut and edit them easily, apply effects, perform advanced sound analysis, and finally export to a plethora of format. The reason I like it so much is that it combines both basic features with more complicated ones, but maintain an easy leaning curve. However, it is not a fully optimized software for hardcore musicians, or people with professional knowledge.

2. Jokosher

On a different level, Jokosher focuses more on the multi-track aspect for musical artists. Developed in Python and using the GTK+ interface with GStreamer for audio back-end, Jokosher really impressed me with its slick interface and its extensions. If the editing features are not the most advanced, the language is clear and directed to musicians. And I really like the association between tracks and instruments for example. In short, if you are starting as a musician, it might be a good place to get some experience before moving on to more complex suites.

3. Ardour

And talking about compex suites, Ardour is complete software for recording, editing, and mixing. Designed this time to appeal to all professionals, Ardour features in term of sound routing and plugins go way beyond my comprehension. So if you are looking for a beast and are not afraid to tame it, Ardour is probably a good pick. Again, the interface contributes to its charm, as well as its extensive documentation. I particularly appreciated the first-launch configuration tool.

4. Kwave

For all KDE lovers, KWave corresponds to your idea of design and features. There are plenty of shortcuts and interesting options, like memory management. Even if the few effects are nice, we are more dealing with a simple tool to cut/paste audio together. It becomes shard not to compare it with Audacity unfortunately. And on top of that, the interface did not appeal to me that much.

5. Qtractor

If Kwave is too simplistic for you but a Qt-based program really has some appeal, then Qtractor might be your option. It aims to be "simple enough for the average home user, and yet powerful enough for the professional user." Indeed the quantity of features and options is almost overwhelming. My favorite being of course customizable shortcuts. Apart from that, Qtractor is probably one of my favorite tools to deal with MIDI files.


Standing for Linux MultiMedia Studio, LMMS is directly targeted for music production. If you do not have prior experience and do not want to spend too much time getting some, go elsewhere. LMMS is one of those complex but powerful software that only a few will truly master. The number of features and effects is simply too long to list, but if I had to pick one, I would say that the Freeboy plugin to emulate Game Boy sound system is just magical. Past that, go see their amazing documentation.

7. Traverso

Finally, Traverso stood out to me for its unlimited track count and its direct integration with CD burning capacities. Aside from that, it appeared to me as a middle man between a simplistic software and a professional program. The interface is very KDE-like, and the keyboard configuration is always welcome. And cherry on the cake, Traverso monitors your resources and make sure that your CPU or hard drive does not go overboard.
To conclude, it is always a pleasure to see such a large diversity of applications on Linux. It makes finding the software that best fits your needs always possible. While my personal favorite stays Audacity, I was very surprised by the design of programs like LMMS or Jokosher.
Did we miss one? What do you use for audio editing on Linux? And why? Let us know in the comments.

How to configure MySQL and MariaDB to accept remote connections

MySQL and MariaDB

Track down connectivity issues to MySQL and MariaDB servers running on Linux.

Connection -- refused
Recently while reviewing MySQL client tools, I discovered a Linux MySQL server on my local network refusing all incoming connections.
To diagnose the problem, I first wanted to determine if MySQL was running on the machine, using the ps command to list processes. I discovered MySQL was running, but unfortunately it was running with command option known as --skip-networking enabled. Reviewing the official documentation for this option, I found that --skip-networking instructs MySQL not to listen for remote TCP/IP connections. This obviously is not ideal if MySQL is needed for use as a database server on a local network.

ps command mysql skip-networking
Because I run Slackware, I suspected that the -skip-networking option was being specified inside the /etc/rc.mysqld script. But as I've often found in similar situations diagnosing Linux issues, knowing beforehand the names of all of script files that execute commands in such a particular way becomes invaluable when tracking down problems.
Finding and fixing
So, I decided to use KDE's built-in Find Files/Folders tool, starting at root (/) to search all files (*.*) and subfolders containing "--skip-networking". After scanning, the search tool verified that rc.mysqld was indeed the only script file containing text matching this particular option.

Searching Linux for files with matching text
With a right-click, I opened rc.mysqld…

Open rc.mysqld for editing
…and quickly found the reference to "--skip-networking" and commented it out, inserting "#" at the beginning of the line.

Use # to comment --skip-networking line
Next, I edited MySQL's configuration file /etc/my.cnf to match the text listed below. I ensured bind-address was set to -- this address instructs MySQL to accept connections from any IP address on my network.
# The following options will be passed to all MySQL clients

 Then, I ran commands to check if the Linux server itself was configured to accept requests on MySQL's port 3306. I used the iptables --list command, but found no references to INPUT port 3306.

iptables --list
So, I added input port 3306, issuing another iptables command:
# iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

iptables adding port 3306
Finally, I restarted MySQL so that the new settings would take effect:

# /etc/rc.d/rc.mysqld restart
Testing the new settings
To test MySQL, I wanted to connect from a different machine on my network. So I turned to a Windows machine running the SQLyog MySQL client. I used the same username and password credentials -- along with port 3306 -- matching the options placed in the my.cnf file on my Linux machine earlier. The MySQL Host Address, is the IP address of my Linux server.

Windows MySQL client settings.

With a click of the Connect button -- all databases on the MySQL server are available for use by the database client app.

List of MySQL databases
This article is published as part of the IDG Contributor Network. Want to Join?