2

I've been doing some research of the best way to show an "users online" counter which is updated to the second trying to avoid continuos ajax polling.

Obviously WebSockets seems to be the best option. Since this is an intranet I will make it a requirement to use Chrome or Safari so there shouldn't be compatibility issues.

I've been reading some articles about WebSockets since I'm new to it and I think I pretty much understand how it works.

What I'm not so sure is how to implement it with PHP. Node.js seems the natural choice for this because of it's "always running" nature but that's not an option.

Why I'm most confused about is the fact that PHP runs and when it's done, it ends. If PHP ended, wouldn't the socket connection be lost? Or if the php re-runs it will look back the user by ip? (I don't see that likely)

Then I found this library http://code.google.com/p/phpwebsocket/ but it seems to be a little old (it mentions only Chrome nightly is compatible with WebSockets)

In one point says "From the command line, run the server.php program to listen for socket connections." which means I need SSH, something many shared hosting plans don't have.

And my other doubt is this other line in the source of that library:

 set_time_limit(0);

does that mean that the php file will run continuously? Is that allow in shared hosting? From what I know all hostings kill php after a timeout of 1 o2 minutes.

I have a mysql table with online users and I want to use PHP to broadcast via websocket the amount of logged in users to those online users. Can someone please help me or point me somewhere with better information how this could be achieved?

Thanks

Juan Ignacio
  • 3,217
  • 8
  • 33
  • 53
  • web-sockets is popular, but server-sent events might be better suited for your purpose, if you can make it work. SSE is apparently possible to support in older browsers with JS, whereas websockets isn't, plus you don't seem to need 2-way communication. I'm trying to work through PHP SSE issues here: http://stackoverflow.com/questions/9070995/html5-server-sent-events-prototyping-ambiguous-error-and-repeated-polling – tomfumb Feb 21 '12 at 02:07
  • I don't have any compatibility issue because I can control the browser used and I was interested in WebSockets because I've seen it work on trello.com and it works like charm – Juan Ignacio Feb 21 '12 at 03:22
  • 3
    My suggestion that you don't try to use PHP for stateful applications as PHP was designed for such in the first place. Also shared hosting might prevent opening arbitrary sockets, as you noted. Your best bet is to get your own VPS server http://www.lowendbox.com/ – Mikko Ohtamaa Feb 21 '12 at 06:25
  • I just tried the exact same setup you described, Juan... I have a shared PHP hosting with SSH access, but the client WebSockets don't connect to the running server on port 12345. I'm pretty sure the shared hosting has a firewall and BLOCKS all ports not 80. – billy Jul 07 '12 at 15:07

1 Answers1

2

Websockets would require lots of thing even on dedicated hosting, put aside shared hosting.

For your requirement server sent events (sse) is the correct choice, since only the server will be pushing data to the client.

SSE can simply call a server script, very much like ajax, but the client side will receive and be able to process data part by part as it comes in. Dom events would be generated whenever some data comes in.

But IE does not support SSE even in version 10. So for IE you have to use some fallback technique like "foreever iframe".

as far as hosting is concerned, ordinary shared hostings (and those which are not very cheap) would allow php scripts to run for long, as long as they are not seen as a problem.

Silver Moon
  • 1,086
  • 14
  • 19