6

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

nvaughan84
  • 463
  • 6
  • 13
  • I would go with node.js and server to client push.. – opHASnoNAME Jan 18 '16 at 10:54
  • Thanks for the comment. I'm looking into using Node with Laravel. In theory I understand how to push messages (in this case, tweets) to a laravel template using Node. I want the users to be managed within the Laravel app and so it will be here that they will select which twitter feeds they want to follow. How would I go about letting node know which users are interested in which feeds and updating their page with the tweets they have selected (rather than globally updating everyone with the same data from Node)? – nvaughan84 Jan 19 '16 at 09:49
  • just some thought: setup an database , share it with the node.js application. Hmm sounds like an interesting project for my off time ;) – opHASnoNAME Jan 19 '16 at 13:46
  • 1
    Rather than have Node access the DB, is it possible for each client to pass an array of search terms to the server (eg ["movies","nintendo","iPhone"]) and then use the Twit module to process each users list and update their feed independently? If so, how does socket update individual clients rather than send the same thing to all connected clients?? Thanks again – nvaughan84 Jan 19 '16 at 17:25

3 Answers3

5

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.

Patrick
  • 3,289
  • 2
  • 18
  • 31
  • Thanks for the reply. Will Long Polling have any impact on the server. If there were 1000 users on the site at one time would this cause any problems for Apache? – nvaughan84 Jan 18 '16 at 11:25
  • Hey - It was good enough for facebook at the time(I think they changed to something else now, depends how big you want to scale). https://www.facebook.com/note.php?note_id=14218138919 – Patrick Jan 18 '16 at 12:21
  • 1
    Another possibly helpful post - http://serverfault.com/questions/562701/how-to-scale-up-a-web-server-supporting-long-polling – Patrick Jan 18 '16 at 12:23
3

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

Imtiaz Pabel
  • 5,307
  • 1
  • 19
  • 24
1

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

Marc Garcia
  • 1,388
  • 1
  • 9
  • 21