0

I built a notification system that checks whether there are new notifications every 10 seconds using the setInverval javascript function, that sends ajax request and returns a json with the notifications.

I would like to hear alternative ways to do that, that doesn't have to be timed.

Is a while loop & a worker thread better? Since I'm working with PHP, how do I not overload the server with too many requests?

What are the security risks I am facing with my system, and with the system you'd like to suggest.

Jacob Cohen
  • 1,232
  • 3
  • 13
  • 33

1 Answers1

1

If you're not satisfied with your current mechanism, look into the cluster of various mechanisms collectively called "comet" and/or web sockets depending on the browser profiles you have to support (IE8 and 9 don't have web sockets, for instance). There are several libraries out there to help you with this, such as socket.io.

I don't think the security profile changes either way, but that would probably be a separate question in any case.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Does php support sockets? – Jacob Cohen May 26 '14 at 09:53
  • @JacobCohen: :-) A quick search for "php websocket" turned up [Ratchet](http://socketo.me/), [`phpwebsocket`](https://code.google.com/p/phpwebsocket/), and this SO question and its answers: [*How to create websockets server in PHP*](http://stackoverflow.com/questions/14512182). – T.J. Crowder May 26 '14 at 10:13
  • Unfortunately, it seems like both are working on the same idea. Client sends a request to the Server, and gets an answer and decides what to do with it. I need the client to keep listening to the server, and when the server sends a message, the client will do what it wants with it. Still reading about socket.io, it might be doing what I need. – Jacob Cohen May 26 '14 at 10:25
  • @JacobCohen: What makes you think that? From the [Ratchet "Process Flow" page](http://socketo.me/docs/flow): *"...once the webpage has been loaded a WebSocket connection is made back to your Ratchet application, where if everything goes correctly, a connection remains open where either the server or client can send data to the other one at any given time."* – T.J. Crowder May 26 '14 at 10:29
  • @JacobCohen: Re socket.io, most of what you'll find is based on a NodeJS backend, but if you search around, there are PHP projects for socketio backends in PHP. I don't know how good they are, never having had to use one. – T.J. Crowder May 26 '14 at 10:30
  • Oh, I might have briefed it too much. I'll read more carefully. – Jacob Cohen May 26 '14 at 10:33