Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real time chat using Ionic and laravel at API end

I am working on some ionic app where laravel is used for api's . There is some demand on having a real time chat functionality .

I have been trying to use something like socket.io .

SO question is without changing stacks , how can i achieve it . My current stacks are cordova , ionic and then laravel mysql for server side .

can this be achieved with current stack ?

Thanks for the help in advance .

Cheers!

like image 449
Anil Sharma Avatar asked Aug 05 '16 11:08

Anil Sharma


1 Answers

Yes, it can be done with this stack - I've done it myself, and I wrote a blog post about how to do so. The Laravel documentation also goes into some detail about this. Our app wasn't using Ionic, but otherwise the situation was basically the same.

The gist of it is as follows:

  • Messages are submitted to the REST API via a POST request as usual
  • When a message is submitted, the controller fires a NewMessage event
  • This event is set as broadcastable, and uses the Redis driver (you may prefer to use Pusher, but I used Redis and Socket.io)
  • A separate Node.js script listens for the NewMessage event, and when it fires, emits the message to all attached clients (or if chat is meant to be private, only the appropriate clients)
  • On receiving the message using socket.io-client, the appropriate action is taken, eg the message is inserted into the DOM

The only addition you need to make to your stack is Node.js and Redis. The biggest issue I had was with configuring Nginx, but that was partly because I was using SSL.

Hope this makes sense. Let me know if you need any more information on how to accomplish this.

like image 109
Matthew Daly Avatar answered Sep 19 '22 00:09

Matthew Daly