sirexzib's avatar

Laravel-Echo-Server send event from client?

I am building a chat module now for my site and I have completed it except the sending.

So my ideal chat module would work like sending an event from the client to the server which then Laravel listens after and when it gets triggered it will handle the event and distribute out the message to both users and log it in the database.

How do I send an event from the client? And how do I listen for it? I find 0 resources or information on how this is done.

I guess I could send the message using ajax but that kinda ruins the whole socket thing and it isn't really optimal for a website with 20000 users.

Any information on how to do this?

For example my JS code could look something like this:

   $(".msg-input").keypress(function (e) {
        if($(this).val().replace(/^\s+|\s+$|\s+(?=\s)/g, "")!="") {
            if (e.which == 13) {
                if($(this).data("dest")!=""){
                    socket.emit('message', {"to":$(this).data("dest"),"msg":$(".msg-input").val() });
                    $(this).val("");
                }
            }else {
                x++;
                if(x==1) {
                    if ($(this).data("dest") != "") {
                        socket.emit('typing', {"to": $(this).data("dest")});
                        x=0;
                    }
                }
            }
        }
    });

socket.emit('message', {"to":$(this).data("dest"),"msg":$(".msg-input").val() });

The above line should send an event like one I made in laravel, like so: event(new EventName('Message here', 'userid') and then I could handle it in an listener from there.

Got any tips on how to achieve this?

0 likes
5 replies
mostafaznv's avatar

I need some information about this matter too. but i didn't find any documentation/information ...

if is there anybody who can help, he will save my life :D I need details about how to send message from client to server, and how to get message in server

mostafaznv's avatar

package developer said it's impossible currently! but I want to implement a chat system and it's important to fire events from client application! is there an alternative?

webivan1's avatar

Echo.connector.socket.emit('EVENT_NAME', 'CHANNEL', { /* DATA OBJECT */ });

davidmirv's avatar

@webivan1 That works great except for the echo server doesn't seem to pass or trigger events to laravel from that..

EhsanDehghani's avatar

@WEBIVAN1 - if I emit using this window.Echo.connector.socket.emit how to listen on it in laravel-echo-server? i want to build something like chat system , but i don't want Laravel or Database to involve for some requests.

i want to send message from client to laravel-echo-server and then laravel-echo-server broadcasts that message to all users.

is there any solution for this?

Please or to participate in this conversation.