Home > other >  ejabberd get chat history of particular users
ejabberd get chat history of particular users

Time:05-25

i built a react client-side app for ejabberd using xmpp-react , everything is working fine so far, I can chat between 2 users, and now I'm trying to fetch the chat history between 2 users, I searched google for this found some examples and articles related to this, here are links I followed

  1. server-setup
  2. xep-0313

here is the code I have tried

await xmppData.send(
  xml(
    "iq",
    {
      type: "set",
      to: "[email protected]",
      from: userData.email,
      id: xmppData.jid._resource,
    },
    xml(
      "query",
      {
        xmlns: "urn:xmpp:mam:1",
        queryid: queryId,
      },
      xml(
        "x",
        {
          xmlns: "jabber:x:data",
          type: "submit",
        },
        xml(
          "field",
          {
            var: "FORM_TYPE",
            type: "hidden",
          },
          xml("value", {}, "[email protected]")
        )
      )
    )
  )
);

in response I'm getting like below

attrs: {xmlns: 'http://jabber.org/protocol/rsm'} children: Array(3) 0: Element attrs: {} children: ['821'] name: "count" parent: Element {name: 'set', parent: Element, children: Array(3), attrs: {…}} [[Prototype]]: Object 1: Element attrs: {} children: ['1648729284714751'] name: "first" parent: Element {name: 'set', parent: Element, children: Array(3), attrs: {…}} [[Prototype]]: Object 2: Element attrs: {} children: ['1652268849881814'] name: "last" parent: Element {name: 'set', parent: Element, children: Array(3), attrs: {…}} [[Prototype]]: Object length: 3 [[Prototype]]: Array(0) name: "set"

the question is why I'm getting the count, last, and first instead of the chat history, and how can I get the previous chats instead of the count, first, last?

any help or suggestions are really appreciated

CodePudding user response:

First of all, use the most simple example query: get all messages stored for your account: https://xmpp.org/extensions/xep-0313.html#example-2

If that works, try to get all the messages stored for your account when conversating with a specific contact: https://xmpp.org/extensions/xep-0313.html#example-6

I know nothing about xmpp-react, so I tested this using a desktop XMPP client.

I use Tkabber, but you can try Gajim, Psi or other well known XMPP client: they usually have a "XML console", or some similar name.

In those windows in those clients you can put XML copied from XEP-0313 (maybe you need to change the JIDs), and see how it works. I tried example 2 and example 6, both return the messages correctly.

You can do like me: try with a well known client, writing the XML stanzas manually, and check they work correctly. Then, you can return to your react client, and try to write similar XML stanzas.

CodePudding user response:

here is below if somebody is trying to get it done,

await xmppData.send(
  xml(
    "iq",
    {
      type: "set",
      to: "[email protected]", // admin email
      from: userData.email, // current user email
      id: xmppData.jid, // jid of the current user
    },
    xml(
      "query",
      {
        xmlns: "urn:xmpp:mam:2",
      },
      xml(
        "x",
        {
          xmlns: "jabber:x:data",
          type: "submit",
        },
        xml(
          "field",
          {
            var: "FORM_TYPE",
            type: "hidden",
          },
          xml("value", {}, "urn:xmpp:mam:2")
        ),
        xml(
          "field",
          {
            var: "with",
          },
          xml("value", {}, toChatWith.recipientName) // another person email
        )
      )
    )
  )
);

getting a response from XMPP

 if (stanza.is("message")) {
      const { children } = stanza;
      const MessageChild = children[0]?.children[0]?.children[0];
      let from = MessageChild?.attrs?.from;
      let to = MessageChild?.attrs?.to;
        setPreMessages((oldMsg) => [
          ...oldMsg,
          {
            msg: MessageChild.children[2].children.toString(),
            sender: from,
          },
        ]);
    }
  • Related