0

I am working on an application where different ajax requests fires depending upon different actions.

For example, there is a chat window having send button. When i click on that button an empty message is sent with ajax, successfully. It work nice. But when I hit the send button too many times, at start some requests respond 200 (ok) but then it respond 500 (internal server error). Due to this the other requests that are going continuously like updateLastActivity also disturb. The preview of the error in developer's tool is: Whoops like something went wrong.

Note: When I make this chat system in core PHP, it work fine. There is no internal server error when I send too may requests.

Here is the code I am using

    //the following code is used to send the message
 $(document).on('click','.send_message_bt',function(event){
     event.preventDefault();

        var id=$(this).data('id');
        var name=$(this).data('name');
        var message=$("#message_field-"+id).val();



    $.ajax({
        //headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
         headers: { 'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!} },
        url:'{{route('user.sendmessage')}}',
        type:'POST',
        data:{
          id:id,
        message:message
           },
        success:function(data,status){
        //clear the message field value
        $("#message_field-"+id).val('');

        //update the chat history
        fetchChatHistory(id,name);


    },
        error:function(response){
           if(response.status==401){
                  alert('You are not logged in!');
                  window.location=window.location.href;
                   }

           }

});

});

here is the back end code

public function sendMessage(Request $request){
    $message=new Userchatmessage();
     $message->message=$request->message;
     $message->sender_id=Auth::user()->id;
     $message->receiver_id=$request->id;
     $message->save();
     return response('success');
}

How to fix this issue.

habib
  • 1,454
  • 17
  • 31

2 Answers2

1

I guess it's not a problem with Laravel or anything, but with your browser. Each browser has a maximum amount of simultaneous connections it will open for a certain domain.

Read more about this problem here and here.

If you want to make a realtime chat application, consider using something like NodeJS and Socket.io.

Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
  • Then why it don't give error when i make the system in core php instead of Laravel. – habib Sep 11 '18 at 08:10
  • Because the problem is with simultaneous requests. A single PHP file will return a response really quick, where a complete framework like Laravel is a little bit slower. – Douwe de Haan Sep 11 '18 at 08:12
  • probably because Laravel goes through a bunch of function call stacks (that may cause php out of memory/timeout) just to process a request, while in your core php, that's just that. – Wreigh Sep 11 '18 at 08:12
  • Can i use sockets for all of my real time work like...making real time notifications...chat...maintain real time online status etc. – habib Sep 12 '18 at 09:46
  • @habib Yes, this is certainly possible! Just read up on some use cases, tutorials and you'll be implementing those things in no time! – Douwe de Haan Sep 12 '18 at 09:48
1

Async and await can help. Let an async function

async function doAjax(){
    await runFirstAjaxCall();
    await runAfterFirstAjaxCallSuccess();
    ....
    ....
}
doAjax();
Daud khan
  • 2,413
  • 2
  • 14
  • 18