WebChat

I'm trying to add in chat a "user is typing" function; chat written in PHP + MySQL/Ajax.

How it should work:

-when my chat partner X starts typing I see in my chat box: "X is typing"

-when I (named Y) am typing he sees in his chat box: "Y is typing" (just like Yahoo Messenger)

i have tried this over several months, but no luck .. please kindly help me

Comments 4

  • You should probably use laravel and pusher or socket.io and redis to handle a real time application. Doing what you want to do would be a real pain in a butt just using PHP and AJAX.

  • For this type of applications is better use Node.js. It's much easier and you can find a lot of tutorials on this topic.

  • Hello,

    First I want to note that to make this event on PHP + Mysql/Ajax is not the best but it is possible, I would suggest to not use it but if it is required here is an idea for this problem.

    The first thing what came in my mind was that if you use Ajax this will require many calls since you need to check every second maybe.

    If the chat system is like on facebook, and you have chat box for every user you are opening then you can set this flag for first.

    I'm user X who is opening the chat for user Y
    First when I open it would be ok to check if the other users chat box is opened for my profile, since maybe you don't want to make Ajax requests if he is not watching. This can be a flag saved in database and updated every time when they open close the chat

    Then when I start typing and the user Y you send a call and save a flag typing.
    From here you can make ajax call on the other ones page and check if the status is typing and show it on client side.

    But I would not suggest to use this kind of solution for chat system since this would be like 3 calls in plus for a not really big feature.

    I still suggest to change the chat system to make it with socket.

    Best regards,
    Tamas

  • var timeout;

    function timeoutFunction() {
    typing = false;
    socket.emit("typing", false);
    }

    $('.typing-message').keyup(function() {
    typing = true;
    socket.emit('startTyping', 'typing...');
    clearTimeout(timeout);
    timeout = setTimeout(timeoutFunction, 3000);
    });

    socket.on('startTyping', function(data) {
    if (data) {
    $('.typing').html(data);
    } else {
    $('.typing').html("");
    }
    });