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?