tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.56k stars 212 forks source link

Multiple messages on 'chat' event #128

Closed ccioloca closed 8 years ago

ccioloca commented 8 years ago

Actual behaviour: client.on('chat', function (channel, user, message, self) {}); On this event`s callback I receive multiple (doubled, tripled) messages.

Expected behaviour: Only one message should be sent on callback

Server configuration

ccioloca commented 8 years ago

I use this on the server side. So I connect multiple identities, and pass info to an internal socket.io My guess is that it creates multiple instances and with each new connection triggers callbacks. Is this correct? Or should I use another approach in this case?

AlcaDesign commented 8 years ago

When you instantiate the client with "new," it creates a new connection to Twitch because you cannot host multiple identities in 1 client. If multiple clients are in the same chat and have the same "chat" callback, you are guaranteed to get doubles and have to handle that yourself.

ccioloca commented 8 years ago

Ok. Got it. Do u have any ideas on how to approach this problem? I could use any help I can get :)

AlcaDesign commented 8 years ago

If all of these identities going to be in the same channel(s) simultaneously: just have 1 of the connections have a callback for the chat and effectively use that chat connection as the main input so you can output to all clients as necessary.

ccioloca commented 8 years ago

I have an internal chat room with users from my app and with users from twitch, and from my app you can also write and receive messages to twitch. So I`m using it both as an internal chat and also as if you were on twitch.

AlcaDesign commented 8 years ago

So the clients could be dropping in and out randomly? I would recommend sending the username of the tmijs client through the socket.io connection and then only displaying the chat out to the matching web client.

ccioloca commented 8 years ago

At some point I used the self param from 'chat' event to filter my messages.

AlcaDesign commented 8 years ago

Server:

var tmi = require('tmi.js'),
    clients = [
            new tmi.client(/* ... */)
            /* ... */
        ];

clients.forEach(function(client) {
    client.on('message', function() {
        var args = [].slice.calls(arguments);
        args.unshift('message', client.getUsername());
        // args == ['message', client.getUsername(), channel, user, message, self]
        io.emit.apply(io, args);
    });
});

Client:

var myName = '...';
socket.on('message', function(theName, channel, user, message, self) {
    if(theName === myName) {
        // This is the chat feed for this person and includes self messages too (for logging at least).
        /* ... */
    }
});
AlcaDesign commented 8 years ago

Socket.io has rooms and namespaces available so perhaps those might be useful instead of sending everyone every message for every client. I haven't ever used that part of Socket.io so I cannot tell you how that would work necessarily.

ccioloca commented 8 years ago

Sweet. Thanks! I`ll check it out.