chrisgeary92's avatar

Real Time, Web Sockets & Laravel?

I really enjoyed the Pusher video a week or 2 ago, but I'm wondering what else there is we can use for more real time applications. I'd really like to learn how to integrate web sockets into my applications. Maybe using something like Ratchet?

0 likes
24 replies
henrique's avatar

This is an interesting tutorial: https://medium.com/laravel-4/laravel-4-real-time-chat-eaa550829538

I started playing with Ratchet for a small script I was doing so javascript could communicate with php that would do the calculations. But I decided to give up and just do everything with javascript to avoid problems I was having (more with the flow I wanted to achieve than with configuring and using Ratchet, which this tutorial was great teaching me on how to use it). If I recall correctly one of the things that got in my nerves was that I used a javascript library (sorry, can't remember the name), which had little docs for v1 and all the nice things for v2, while Ratchet worked only with v1.

1 like
ekaitzastiz's avatar

An easy way (not optime, I have just play with it, If there some one who has really use it and can help or give us some basics (loop, security... react php, ratchet) to try something with sockets Intall brainsocket and brainsocket js:

http://brainsocket.brainboxmedia.ca/ https://github.com/BrainBoxLabs/brain-socket https://github.com/BrainBoxLabs/brain-socket-js

(http://userscape.com/laracon/2014/jeremymikola.html )

Example: notify who is reading the same post. My question and what I don't know yet is how to improve not to have so many conection open, or is it very bad...I mean, X (1000) people reading, X opened connections. May be another way and better to use SSE?

a view:

{{ HTML::script('js/BS/brain-socket.min.js') }}


<script type="text/javascript">

    $(document).ready(function(){
        window.app = {};

        app.BrainSocket = new BrainSocket(
            new WebSocket('ws://localhost:8080'),
            new BrainSocketPubSub()
        );



        app.BrainSocket.Event.listen('post-<?php echo $artikulu->id ?>',function(msg)
        {
            console.log(msg);
            var me = {{ Auth::user()->id }} ;
            if(msg.client.data.user_id != me  )
                $('#widget-content-1').append('<div class="ohar-mezua alert-success">'+msg.client.data.message+'</div>');
        });


        //$('#chat-message').on('click',function(e) {
            setTimeout(function() {
        app.BrainSocket.message('post-<?php echo $pos->id ?>',
            {
                'user_id':'{{ Auth::user()->id}}',
                'message':'{{ Auth::user()->alias}}  is reading this post'
            }
        );
        },3000);

in a controller function (show($id) )

public function show($id)
{

$post = $this->postRepo->find($id);

  Event::listen('artikulu-'.$id,function($client_data){
            return BrainSocket::message('artikulu-'.$id,array('user_id'=>100,'message'=>'A message from a generic event fired in Laravel!'));
        });


return View::make()->with(compact('post'));

}

I don't know why, but I can't get the message() sent by the server. So that should be fixed, otherwise, I suppose any one can change the JS and send any message...And in my case I don't want to do a chat room...laughs.

2 likes
Devon's avatar

I'd love to see a few lessons on working with WebSockets... Perhaps even WebSockets, AngularJS, and Laravel for the backend logic.

7 likes
meetgodhani's avatar

Plus point in using Pusher is that you can easily integrate with most of the code languages/libraries. Like AngularJS.

someguy123's avatar

@hfalucas that would be pretty awesome if he did. I never knew that you could use Laravel with Websockets effectively. In most Laravel applications we "use the right tool for the right job", which normally involves either Python+Twisted/Tornado, or NodeJS to handle the web sockets, with a Redis queue linking the two applications together.

1 like
hfalucas's avatar

I was thinking about implementing something with Node.js then came across with that package @someguy123. Never used it before so I don't know if it's good or not, hopefuly will have time to play arround with it this weekend :)

DivDax's avatar

+1 for a real time chat lesson by jeff :) Would be cool to use a self hosted chat server.

2 likes
sephvelut's avatar

Pusher is nice but I try to avoid vendor bloat. Would be nice to have a video on socket.io + laravel.

1 like
russw's avatar

BrainSocket looks really cool but I don't think it has support for Laravel 5 right now, so I wouldn't want to depend on it too much for new projects.

paplero79's avatar

You can find a tutorial on SocketIO with Redis and Laravel 5 here . It work very nice but i think would be better to use the php-emitter. Probably another tutorial will come soon.

1 like
xsmalbil@icloud.com's avatar

Watch out with using Ratchet and Autobahn. Ratchet supports WAMP v1 and Autobahn.js (latest) is supporting WAMP v2.

msalom28's avatar

socket.io and laravel play really nice together. I created small social network using these technologies: http://larasocial.info The code is on github. Let me know if you have questions.

2 likes
ddeath's avatar

@msalom28 it is not possible to sign in - Your profile picture is required... even when I choose picture

msalom28's avatar

@ddeath I am able to login with no problems. Try to login with one of the sample accounts.

megamitch's avatar

I've been using Ratchet for my Laravel Chat App, it's easy to install and easy to configure to your likings. I recommend Ratchet.

Please or to participate in this conversation.