2

I'm very new to server push technologies (websockets, socket.io, sockjs) so this is probably a very newbie question. However, my google-fu has not been strong enough to find the answer.

I'm writing, using PHP server-side and javascript client-side, an "event ticker" application. It works like this:

  1. When the user logs in, the server sends him the last 10 events. This is easy!
  2. Then, the server sends the user new events as they come in. This is what I'm having trouble figuring out.

Right now, I have the events stored in a database. How does the server figure out which events are "new" and which are "old" (i.e. have already been sent to the client)? Is it as simple as having a boolean column in the DB for each event?

Edit: To clarify my question: I know how to set up the server push client side... actually, websockets, sockjs and socket.io all have identical API, they were designed that way. I also know how to set up PHP to run in an event loop. There are tutorials on this. Python, Ruby and Javascript don't have native support for this either, and use non-native frameworks to do it.

My question is specifically about the details of the database interaction: for example, how does my server-side script know that the database has been updated? Polling the database is the only solution that I can think of, but that seems non-optimal. I'm using MySQL, so it's not an event-driven database. The reason I'm using MySQL is that the rest of the website uses MySQL. Bad choice? Should I be looking at transitioning to a different DBMS? Or can MySQL be made to work in this application?

xtpu
  • 55
  • 1
  • 5

1 Answers1

0

As with almost everything, there's more than one way to achieve this, but with your description it does seem as though websockets is the way to go.

In the browser side, check whether websocket is supported and if so add the code:

var ws = new WebSocket("ws://YOUR_HOST/your_path/");
ws.onopen = function () {};
ws.onmessage = function (evt)
{
    render_events(evt.data);
};
ws.onclose = function () {};

where render_events will be your browser side implementation to render the new events in the browser.

Regarding the server side, deciding which events to send in principle is as simple as you described, however since plain vanilla PHP is not meant to run in an event loop but just process a request and exit then in order to scan for changes in the database and send them out to the clients perhaps you should try reactphp or php libevent though I have no experience with either.

Another alternative is using Python or some other language that is more suited for waiting and responding to events.

Amnon
  • 2,708
  • 2
  • 22
  • 21
  • To clarify my question (I'm going to edit the original question above): I know how to set up the server push client side... btw, websockets, sockjs and socket.io all have identical API, they were designed that way. I also know how to set up PHP to run in an event loop. There are tutorials on this. Python, Ruby and Javascript don't have native support for this either, btw, and use non-native frameworks to do it. – xtpu Apr 02 '14 at 13:04
  • 1
    My question is specifically about the details of the database interaction: for example, how does my server-side script know that the database has been updated? I'm using MySQL, so it's not an event-driven database. The reason I'm using MySQL is that the rest of the website uses MySQL. Bad choice? Should I be looking at transitioning to a different DB? Or can MySQL be made to work in this application? – xtpu Apr 02 '14 at 13:06
  • Isn't the server the one that receives the trigger to update the database ? Couldn't that same trigger be used to update the clients ? If not then perhaps something like this http://stackoverflow.com/questions/1467369/invoking-a-php-script-from-a-mysql-trigger would help, but it's an interesting question. – Amnon Apr 02 '14 at 20:51