adding custom attribute to message not storing message in Archive table on server using stanza.io

351 Views Asked by At

I am working on ionic-framework and i'm using stanza.io library to implement chatting with xmpp server, I want to add some custom attributes while sending message, for that i have followed the steps for creating plugin. my code is as follow...

sendMsg() {
    console.log("Sending message");


    function customMessage(client, stanzas) {
      const NS = 'http://www.w3.org/2005/Atom';
      var types = stanzas.utils;

      const messageAttribute = stanzas.define({
        name: 'messageAttribute',
        element: 'messageAttribute',
        namespace: NS,
        fields: {
          title: types.textSub(NS, 'title'),
          summary: types.textSub(NS, 'summary'),
          published: types.textSub(NS, 'published'),
          updated: types.textSub(NS, 'updated'),
          cont: types.textSub(NS, 'cont')
        }
      });

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

    this.client.use(customMessage);

    this.client.sendMessage({
      to: this.recep,
      body: "",
      messageAttribute: {
        'title': "some title",
        'summary': "message",
        'published': "time stamp here",
        'updated': "time stamp here",
        'cont': "cht"
      }
    });
   console.log("Message sent " + this.sMsg);
  }

but doing this way message are not being stored in Archive table on server. that will create a problem to get the history from the server. if we use the simple code then that messages being stored on Archive table on server. simple code as follow..

this.client.sendMessage({
      to: this.recep,
      body: this.sMsg    
    });

in simple code we can only send message as a string inside body. can anyone help me to solve this?

2

There are 2 best solutions below

0
On BEST ANSWER

My server is only archiving messages which contain a body element with text, which is a pretty common archiving configuration. One trick would be trying to include a dummy body text to trigger message archiving, but you'd have to check if the server is storing and returning the full stanza or just extracting and saving the body text.

done everything correctly with extending Stanza to include the additional fields, but need to adjust server to get what i want. confirmed from here.

0
On

You need to add an extra param store in message stanza which makes the message to store in Archive table by default.

const store = stanzas.define({
  name: 'store',
  element: 'store',
  namespace: 'urn:xmpp:hints'
}); 

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

Sent store attribute as true in message stanza

this.client.sendMessage({
  to: this.recep,
  body: this.sMsg,
  store: true
});

You should see store inside message stanza like

<store xmlns='urn:xmpp:hints'/>