Closed ccioloca closed 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?
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.
Ok. Got it. Do u have any ideas on how to approach this problem? I could use any help I can get :)
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.
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.
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.
At some point I used the self param from 'chat' event to filter my messages.
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).
/* ... */
}
});
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.
Sweet. Thanks! I`ll check it out.
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