Push notification to the client browser

32,124

Solution 1

As you want to implement this in CakePHP (so I assume it's a web-based application), the user will have to have an 'active' page open in order to receive the push messages.

It's worth looking at the first two answers to this, but also just think about how other sites might achieve this. Sites like Facebook, BBC, Stackoverflow all use techniques to keep pages up to date.

I suspect Facebook just uses some AJAX that runs in a loop/timer to periodically pull updates in a way that would make it look like push. If the update request is often enough (short time period), it'll almost look realtime. If it's a long time period it'll look like a pull. Finding the right balance between up-to-dateness and browser/processor/network thrashing is the key.

The actual request shouldn't thrash the system, but the reply in some applications may be much bigger. In your case, the data in each direction is tiny, so you could make the request loop quite short.

Experiment!

Solution 2

There are several ways you can accomplish this. The most supported way is through a technique called Comet or long-polling. Basically, the client sends a request to the server and the server doesn't send a response until some event happens. This gives the illusion that the server is pushing to the client.

There are other methods and technologies that actually allow pushing to the client instead of just simulating it (i.e. Web Sockets), but many browsers don't support them.

Solution 3

Standard HTTP protocol doesn't allow push from server to client. You can emulate this by using for example AJAX requests with small interval.

Solution 4

Have a look at php-amqplib and RabbitMQ. Together they can help you implement AMQP (Advanced Message Queuing Protocol). Essentially your web page can be made to update by pushing a message to it.

[EDIT] I recently came across Pusher which I have implemented for a project. It is a HTML5 WebSocket powered realtime messaging service. It works really well and has a free bottom tier plan. It's also extremely simple to implement.

Solution 5

Check out node.js in combination with socket.io and express. Great starting point here

Share:
32,124

Related videos on Youtube

Harsha M V
Author by

Harsha M V

I turn ideas into companies. Specifically, I like to solve big problems that can positively impact millions of people through software. I am currently focusing all of my time on my company, Skreem, where we are disrupting the ways marketers can leverage micro-influencers to tell the Brand’s stories to their audience. People do not buy goods and services. They buy relations, stories, and magic. Introducing technology with the power of human voice to maximize your brand communication. Follow me on Twitter: @harshamv You can contact me at -- harsha [at] skreem [dot] io

Updated on May 08, 2020

Comments

  • Harsha M V
    Harsha M V almost 4 years

    I'd like to create an application where when a Super user clicks a link the users should get a notification or rather a content like a pdf for them to access on the screen.

    Use Case: When a teacher wants to share a PDF with his students he should be able to notify his students about the pdf available for download and a link has to be provided to do the same.

  • jellyfishtree
    jellyfishtree about 13 years
    Using Comet style techniques (ie, polling, etc)...good article for reference here webreference.com/programming/javascript/rg28
  • Ant Kutschera
    Ant Kutschera almost 13 years
    HTTP doesnt restrict you - if you keep the connection alive, the server can indeed push data to the client at some time in the future. You wouldn't normally do it. But you could, if your use-case made sense. Continually polling for data when most of the time it will result in no response/update creates load on the server which may make you opt for a true push solution.
  • user115422
    user115422 over 11 years
    i use 5k milliseconds, it does the trick for me even with 128mb of ram
  • user965369
    user965369 about 11 years
    I know this is an old question but I'm curious.. What about some sort of TCP socket connection that pushes data to a browser? Small data packets sent via some sort of AJAX-type TCP connection ... ?
  • Leo
    Leo about 11 years
    I don't think you can do that. Even if you could it could potentially lead to a situation where you had tens of thousands of tcp sockets open.
  • James Cowhen
    James Cowhen almost 11 years
    I think this is the easiest and well rounded solution. Socket.io degrades nicely from websocket -> Flash -> long polling. You can just implement the chat on nodejs while keeping your app in the language of your choice.
  • Mr_Green
    Mr_Green almost 8 years
    How this is related to PHP?

Related