Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Making a Real Time Application using Angular

I am starting to work with angular and am fascinated by the bi-directional data-binding capabilities and by its $http method, which lets me save changes in to my mysql database, without refreshing the page.

Another thing I am currently fascinated by is the real time capability across multiple clients using firebase. Here all clients are updated in REAL TIME, when the database receives any changes. I'd probably like to use firebase, but I would have to drop Laravel and MySql as a persistence layer entirely, which I would like to keep for the moment, since my application is already working in Laravel, just not in real time.

How would I go about having a Real Time application, which updates every client, without refreshing the view, in Laravel using MySQL and Angular?

If I am not mistaken, Pusher and PubNub, are providing this necessary open connection with the server using websockets, so when the server has something to share, angular will now and render it.

Since I would like to use Laravel and MySQL as a persistence layer, I am not sure, what the best way would be. I am not even sure, if I understood everything correctly, which I wrote above, since I am new to angular and real-time applications.

What would be the next necessary steps, to get some Real-Time capability into a PHP/MySQL application?

like image 458
LoveAndHappiness Avatar asked Oct 01 '22 07:10

LoveAndHappiness


2 Answers

The solution for your problem is:

1º - open websocket connection with the websocket-server and subscribe a channel, after this send the data to your serve using ajax tutorial angular pusher

2º - In server side, you get the data, saves to your database and send a 'PUBLISH' to the respective channel into websocket server lib useful for this

3º - Through the subscribe gets the data in real time

Pusher.subscribe('channel', 'event', function (item) {
    // code
});
like image 64
Claudivan Avatar answered Oct 21 '22 15:10

Claudivan


I had a similar problem recently and I finally ended up using Redis publish/subscribe Redis. You can store data in the channel and then subscribe to any changes. When something changes you can send it to Pusher which will send it then to the clients.

I also recommend considering Node.js and Socket.io since you can achieve very good performance without third party service, and even if you don't have experience with node you can find very good examples on Socket.IO how to write an application.

For Redis there is a good library for PHP called Predis and there is Redis Node client as well, so you can mix it all together.

like image 32
schrink Avatar answered Oct 21 '22 14:10

schrink