vendredi 25 décembre 2015

get the last two seconds records of chat using laravel,mysql and angualr js

Hi i am creating a chat box like facebook what i want to do is to get the last two seconds chat messages like we have in the facebook when some fiend on facebook send you a message then if your chat box is open then it appends the message in the end of the box.\

but i am having problem here i am using agularjs and laravel when i send some message and the request runs then it mostly append the messages two times in the chat box.like i say hello to other user then it will show two times hello their in box here is my html

<ul class="chatbox" ng-repeat="chatData in chatboxData">
    <li>
        <div class="message">
            <a href="javascript:;" class="name"> {{ chatData.sender_fname }} </a>
            <span class="body"> {{ chatData.message }} </span>
        </div>
    </li>
</ul>

here is angualr js

$scope.showSelecteduserChat = function(chat_id) {
    $http.get(url+'/chatboxChat/'+chat_id).success(function(data){
        $scope.chatboxData = data;
         $interval( function(){ $scope.getlasttwosecMsgs(user_id_chat,chat_id,user); }, 2000);

    });
}

and code to get the last two seconds records are

$scope.getlasttwosecMsgs = function(chat_id) {
    $http.get(url+'/getlasttwosecMsgs/'+chat_id).success(function(data){
        angular.forEach(data, function(value, key) {
          $scope.chatboxData.push(value);
        });
    });
}

i am also getting the distinct records from query here is my query

$data = DB::table(MESSAGES_TBL)
                ->join(USERS_TBL.' as us', MESSAGES_TBL.'.sender_id', '=', 'us.id')
                ->join(USERS_TBL.' as ur', MESSAGES_TBL.'.receiver_id', '=', 'ur.id')
                ->where(MESSAGES_TBL.'.id_chat_room',$chat_id )
                ->whereRaw("message.date_created > (now() - INTERVAL 2 SECOND)" )
                ->orderBy(MESSAGES_TBL.'.created_at', 'ASC')
                ->select(MESSAGES_TBL.'.id as msg_id',MESSAGES_TBL.'.sen_rec_id',MESSAGES_TBL.'.sender_id',MESSAGES_TBL.'.receiver_id',MESSAGES_TBL.'.created_at',MESSAGES_TBL.'.message',MESSAGES_TBL.'.date_created')
                ->distinct()
                ->get();

            return json_encode($data);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire