Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Real Time updates in a Laravel App

I'm building a web application where users can sign up, add twitters feeds that they want to follow and their stream will update as the feeds they're following receive new posts.

My go to platform is Laravel. However, I can't think of the best way to implement the live update aspect of the site.

I would use an AJAX function that is called periodically (every 30 seconds for example) but as the number of users increases this method as it's drawbacks.

I've looked into HTML5 Server Side Events but IE isn't supported unfortunately.

What would be the best way to implement this functionality within a Laravel App?

Thanks,

Nick

like image 695
nvaughan84 Avatar asked Jan 18 '16 10:01

nvaughan84


3 Answers

You have two options :

  1. Streaming ( Websockets )
  2. Long polling

You can read more about websockets here : https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

And you can read more about long polling here : https://www.quora.com/Why-would-HTTP-long-polling-be-used-instead-of-HTTP-Streaming-to-achieve-real-time-push-notifications

In short :
websockets run on a different port than your usual app, so accessing all your assets can be a bit strange(depending on your system architecture).

Long polling is a very long http request that can last up to several minutes, instead of sending a request every 30 seconds, you send it every time the server returns a response. this means that if the server took 5 minutes to return a response, you're only sending a request once per 5 minutes. (for example, there's no reason to alert the client that nothing changed at all, so you can sleep(30) and try again)

As a side note, Unless you need real time data, I think long polling is much easier to implement and use with a framework such as laravel.

like image 196
Patrick Avatar answered Oct 18 '22 11:10

Patrick


you can use pusher or node.js for realtime.In laracast you will find a video how to do that https://laracasts.com/series/intermediate-laravel/episodes/4

like image 26
Imtiaz Pabel Avatar answered Oct 18 '22 10:10

Imtiaz Pabel


I use Pusher for real time data, is extremly easy to use and exists a Laravel package.

https://pusher.com/

https://github.com/vinkla/pusher

like image 28
Marc Garcia Avatar answered Oct 18 '22 09:10

Marc Garcia