5

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!

Anil Sharma
  • 2,952
  • 5
  • 29
  • 45
  • Possible duplicate of [Simple websocket implementation in laravel 5](http://stackoverflow.com/questions/33471695/simple-websocket-implementation-in-laravel-5) – Fabio Antunes Aug 05 '16 at 12:06

1 Answers1

6

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.

Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
  • That makes sense . But when on client end if i will go with redis , i will have to set up node script which can listen for broadcasted event from redis. Where i will set up this node server ? communication is like android(cordova) to server(laravel) . and we need bidirectional communication b/w them , where this node script will reside and node server will be set up ? Hope you are getting my curiosity comes confusion . lol . – Anil Sharma Aug 05 '16 at 12:46
  • 1
    The Node.js script can run on the same server quite happily, it just needs to listen on a different port, and Nginx can reverse proxy requests to the Node.js script to that port. My experience was that it was best to let Laravel handle receiving the messages via HTTP, and just broadcast them on receipt - it wasn't terribly practical to send and receive messages via Websockets without switching the whole thing over to Node.js, and this was a perfectly acceptable compromise. – Matthew Daly Aug 05 '16 at 12:53
  • 1
    Basically the flow is that Laravel pushes the event to Redis, the Node.js script picks up the event and broadcasts it via Socket.io, and the app picks it up via the Socket.io client – Matthew Daly Aug 05 '16 at 12:54
  • 1
    This approach does mean that you're sending messages via a different method to the one used to receive them, which isn't ideal, but I found it to be the best way to do it while still using Laravel. – Matthew Daly Aug 05 '16 at 13:02
  • Yes that makes sense , without altering the flow , so node can reside on same server where laravel is , simple when we specify node server address to socket we will write something like . domain.com:3000(or some other node port depends) . – Anil Sharma Aug 05 '16 at 13:09
  • No, you don't need to specify the port (at least not if you're using Nginx as a reverse proxy). Just configure Nginx to reverse proxy anything where the location starts with `/socket.io` to the upstream Node.js script. There's an example Nginx config and client-side script for connecting to Socket.io in my blog post. What will happen is when your app sets up the Socket.io connection, the initial handshake will be done over HTTP or HTTPS, and then the connection gets upgraded to Websockets. – Matthew Daly Aug 05 '16 at 13:20
  • Ok that is fine , but right now i am using apache as webserver . so without switching to nginx , i can run node server on same server on some port , and use that url , make sense? – Anil Sharma Aug 05 '16 at 13:33
  • That should work fine, but as I used Nginx I can't guarantee it will work as well with Apache. Your best bet is probably to try it in a VM. – Matthew Daly Aug 05 '16 at 13:48