0

I'm new to Socket.io and I'm writing a program to connect with a chat agent.

Before posting this question I've referred the below questions, but, unfortunately didn't find a solution.

  1. socket.io emits multiple times
  2. Socket.io returning multiple times for one event
  3. socket.on event gets triggered multiple times
  4. Socket.io message event firing multiple times

Here is my client side code.

//Connect
var socket = io.connect('http://localhost:4000');

//Query Dom
var message = document.getElementById('message'),
    handle = document.getElementById('handle'),
    btn = document.getElementById('send'),
    output = document.getElementById('output'),
    feedback = document.getElementById('feedback');


//Emit Events
btn.addEventListener('click', () => {
    if (message.value === 'Transfer') {
        console.log(JSON.stringify(message.value));

        socket.emit('transfer', {
            message: message.value,
            handle: handle.value
        });
    }
    if (message.value !== 'Transfer') {
        console.log(JSON.stringify(message.value));

        socket.emit('U_chat', {
            message: message.value,
            handle: handle.value
        });
    }

});

//Listen for Events
socket.on('A_chat', data => {
    // console.log(JSON.stringify(data));
    feedback.innerHTML = '';
    output.innerHTML += `<p><strong>${data.handle}:</strong> ${data}</p>`
});
socket.on('U_chat', data => {
    //console.log(JSON.stringify(data));
    feedback.innerHTML = '';
    output.innerHTML += `<p><strong>${data.handle}:</strong> ${data}</p>`
});

and here is my server side code.

var io = socket(server);

io.on('connection', function (socket) {
    console.log(`Connection Established ${socket.id}`);
    socket.on('transfer', (data) => {
        console.log(`TeNSFER TO Agent`);
        main(socket);
    })
});


//LA Communication
async function main(socket) {
    console.log(`Entereed`)
    const getSessionId = await helperFunctions.sessionId();
    var tempVar;
    if (getSessionId.success === true) {
        sessionkey = getSessionId.data.key;
        affinity = getSessionId.data.affinityToken;
        sessionid = getSessionId.data.id;

        const body = {
            //body params
        };

        const sendingChatRequest = await helperFunctions.sendingChatRequest(
            body,
            affinity,
            sessionkey
        );

        if (sendingChatRequest === true) {
            pullmessageorg = await helperFunctions.pullingMessages(
                affinity,
                sessionkey
            );

            while (pullmessageorg.messages[0].type != "ChatEnded") {

                if (pullmessageorg.messages[0].type === "ChatRequestSuccess") {
                    io.sockets.emit('A_chat', ' Waiting for agent to accept your request.');
                }
                if (pullmessageorg.messages[0].type === "ChatEstablished") {
                    io.sockets.emit('A_chat', 'Agent Accepted');
                    io.sockets.emit('A_chat', ' Available');
                }

                if (pullmessageorg.messages[0].type === "ChatMessage") {
                    io.sockets.emit('A_chat', pullmessageorg.messages[0].message.text);
                    socket.on('U_chat', async (data) => {
                        io.sockets.emit('U_chat', data.message);
                        let text = data.message;
                        const sendMessage = await helperFunctions.sendMessages(
                            text,
                            affinity,
                            sessionkey
                        );
                    })
                }
                const pullingMessagesAgain = await helperFunctions.pullingMessages(
                    affinity,
                    sessionkey
                );
                pullmessageorg = pullingMessagesAgain;
            }
            return;
        } else {
            return;
        }
    } else {
        return;
    }
}

When I run this program, the number of times client messages are getting printed are the total number of server messages.

For eg: Server: Hi Client: Hi Server: what's up Client: Nothing Much Client: Nothing Much Server: Anything Else? Client: No Client: No Client: No

I'm unable to understand where I'm going wrong. Also I tried using socket.once, unfortunately, Unable to send multiple messages, like below. Client: Hello (this is getting emitted) Client: How are you? (this is not getting emitted)

Please let me know where Am I going wrong.

Thanks

user3872094
  • 3,269
  • 8
  • 33
  • 71

0 Answers0