Stanza.io send message with customer attributes in ReactJS

938 Views Asked by At

I want to sendMessage with custom attributes in ReactJS / Stanza.io . I am unable to make it working. Any good working example?

1

There are 1 best solutions below

0
On BEST ANSWER

Suppose I have to send a message with custom attribute named as custom :

<message to="tom@example" id="273z4-567" type="chat" from="john@example"> 
   <body>hi</body> 
   <custom xmlns="xmpp:customAttr" layout="testLayout"> // custom attribute
      <value>1234</value> 
   </custom>
</message>

you can do it like :

const XMPP = require('stanza.io');

let client = XMPP.createClient({}); // obj with config
client.use(this.setCustomMessageAttributes());


setCustomMessageAttributes() {
    const NS = 'xmpp:customAttr';
    const customAttribute = stanzas.define({
        name: 'custom',
        element: 'custom',
        namespace: NS,
        fields: {
            value: stanzas.utils.textSub(NS, 'value'),
            layout: stanzas.utils.attribute('layout')
        }
    });

    stanzas.withMessage((Message) => {
        stanzas.extend(Message, customAttribute);
    });
}

You can send message as

client.sendMessage({
    to: "jid",
    body: "hi",
    custom: {
        value: "1234",
        layout: "testLayout"
    }
});

You can also refer to https://github.com/legastero/stanza.io/blob/master/docs/Create_Plugin.md

If you are still facing issues, paste your code here.