vp-platform
vp-platform

Reputation: 601

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).

Upvotes: 5

Views: 4068

Answers (2)

beaver
beaver

Reputation: 17647

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);
}

Upvotes: 5

moose
moose

Reputation: 162

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.

Upvotes: 4

Related Questions