4

How to add on-message handler for strophe MUC plugin.

Currently i added callback function for join action.

Gab.connection.muc.join(room_name+"@muc.162.242.222.249",  login_id, 
function(message){ 
ArK
  • 20,698
  • 67
  • 109
  • 136
user1752065
  • 179
  • 9

1 Answers1

4

You can check message types in your general message handler:

connection.addHandler(onMessage, null, 'message', null, null, null);

...

function onMessage(msg) {
  var to = msg.getAttribute('to');
  var from = msg.getAttribute('from');
  var type = msg.getAttribute('type');
  var elems = msg.getElementsByTagName('body');

  if (type == "chat" && elems.length > 0) {
    var body = elems[0];
    console.log('CHAT: I got a message from ' + from + ': ' + Strophe.getText(body));
  } else if (type == "groupchat" && elems.length > 0) {
    var body = elems[0];
    var room = Strophe.unescapeNode(Strophe.getNodeFromJid(from));
    var nick = Strophe.getResourceFromJid(from);
    console.log('GROUP CHAT: I got a message from ' + nick + ': ' + Strophe.getText(body) + ' in room: ' + room);
  }
  // we must return true to keep the handler alive.  
  // returning false would remove it after it finishes.
  return true;
}
beaver
  • 17,333
  • 2
  • 40
  • 66
  • Thanks for the Reply .. Now incoming messages working after adding this line. connection.addHandler(Gab.on_message, null, "message", "groupchat"); – user1752065 Jul 20 '16 at 11:37
  • Now the Next thing i want is message history for a user(When user click on joined group) . Currently i have the following code for handling archive . connection.mam.query( Strophe.getBareJidFromJid(Gab.connection.jid), { with : jid+"@muc.server", max: 50, before: '', onMessage: function (message) { – user1752065 Jul 20 '16 at 11:39