Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom XMPP messages with Strophe.js

How can I send custom messages with XMPP using the Strophe JS library?

I know that using $msg( ... ); I can create a chat message element and connection.send(m); send it through XMPP connection.

I need a way to send messages not for chat but for "command" (or other purpose).

like image 345
vp-platform Avatar asked Feb 03 '16 12:02

vp-platform


2 Answers

Using Strophe.js you can simply do:

function sendCustomMessage(to, from, body, field1, field2) {
    var m = $msg({to: to, from: from, type: 'chat'}).c("body").t(body);     
   // custom data
   m.up().c("data", {xmlns: 'my-custom-data-ns', field1: field1, field2: field2});
   connection.send(m);
}
like image 67
beaver Avatar answered Nov 17 '22 05:11

beaver


In XMPP you can add custom payload in the XML stanza, for example:

<message id="xyz" type="chat" to="tojid@com" from="fromjid@com">
    <body>....</body>
    <data xmlns='mycustom-data-ns'
      myField1="bye" myField2="data" />
</message>

Check on Strophe.js docs how to create that msg.

like image 27
moose Avatar answered Nov 17 '22 06:11

moose