How retrieve Chat History using Java Smack library from openfire server?
Asked Answered
C

5

8

After installing Open Archive plugin in the Openfire server I can see the chat conversation between two user from the openfire admin panel which is pretty easy and that is web based too. Now I want to retrive those conversation or chat history from chat client application(written in java) where I've used Smack library. I didn't found any helpfull resource for that. Any advice will be helpfull.

Ciri answered 21/6, 2015 at 6:0 Comment(2)
Is your problem solved ? I am facing this tooErroneous
You need to retrieve the chat history using your own custom implementation, in my case I had own central web server which can give me the data from the database.Ciri
C
1

Finally I got the answer. Archive Messaging features are currently not implemented in Smack library.

https://community.igniterealtime.org/message/249993#249993

Ciri answered 23/6, 2015 at 9:18 Comment(6)
hi Dhiren Hamal, how to use XEP-0136 Archive Messaging to get the chat history in my android application .Spleen
@Spleen ..Did u get any solution to get the chat history?. Please let me know .. Thanks :)Lucullus
@DhirenHamal .. Did u get any solution to get the chat history?. Please let me know .. Thanks :)Lucullus
@Dhiren Hamal .. i didnt found the solution yet.. still searching :(Spleen
@Spleen you can use Open Archive plugin into your openfire server which automatically archive the messages between users conversation and after that you've to pull that stores messages from your client site. Meant to say that I've used spring web server to retrive offline messages.Ciri
@DhirenHamal Can you help with some code snippet. I am using smack api to implement the same.Wendy
C
6

Smack just implemented MAM feature [XEP 0313] but yet not released, hope to get it on next release if you want to use this feature build the smack library from source or you can use custom IQ to get archived messages from server.

Conditioning answered 14/8, 2016 at 11:3 Comment(14)
is it added now? @Sakib in version 4.2.0 . How will I check it?Racine
@AnkurKhandelwal Please check their release change log or issues in github.Conditioning
@AnkurKhandelwal Hi Ankur, I am also working for the same task. Are you able to find out the api and get the Chat messages ?Wendy
@Wendy download.igniterealtime.org/smack/docs/latest/documentation/… I think this is api that is used to fetch previous message. I am able to get the stanza id of the all the previous message. Now I don't know how to parse id to actual message text. If you got any clue let me also know.Racine
@AnkurKhandelwal Can you please help me with the code that you used. I tried to implement the code but getting "java.lang.IllegalArgumentException: Must have a local (user) JID set. Either you didn't configure one or you where not connected at least once". Don't know where i have to pass it. Please help.Wendy
var mamManager:MamManager= MamManager.getInstanceFor(connection) //by jid var prevMsg=mamManager.queryArchive(JidCreate.entityBareFrom(jid)).forwardedMessages var prevFin=mamManager.queryArchive(JidCreate.entityBareFrom(jid)).mamFin for (msg in prevMsg){ println(msg.forwardedStanza) println("Id is "+msg.forwardedStanza.stanzaId) println("From "+msg.forwardedStanza.from) println("\n") }Racine
@AnkurKhandelwal In my case issue is somewhere in XMPPTCPConnectionConfiguration object creation and when i use mamManager object to check isSupport in the internal api at some place it checks for conn.getUser which returns null Due to this it's throwing "java.lang.IllegalArgumentException: Must have a local (user) JID set. Either you didn't configure one or you where not connected at least once". Can you please let me know how you created XMPPTCPConnectionConfiguration object.Wendy
val config= XMPPTCPConnectionConfiguration.builder() .setXmppDomain(mDomainName) .setUsernameAndPassword(mUsername,mPassword) .setDebuggerEnabled(true) .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) mConnection= XMPPTCPConnection(config.build()) mConnection!!.connect().login() Racine
@AnkurKhandelwal Now I am also stuck at the same place.Got the stanza id but don't know how to use it.Wendy
Let us continue this discussion in chat.Racine
@Wendy are you able to receive the group message?Racine
@AnkurKhandelwal I am only fetching one to one chat and i am able to do . Smack API is not returning any forward message for me. So uses the monitoring plugin, modified it and my purpose is solved.Wendy
Monitoring plugin?? sorry i am not able to get you @WendyRacine
It's a openfire plugin with name "Monitoring Service".I used it's source code and wrote my own plugin for my requirement.igniterealtime.org/projects/openfire/plugins.jspWendy
M
4

It might be a late answer but now as SMACK API supports XEP-0136 and XEP-0313, so below code can help people landing to this page.

public MamManager.MamQueryResult getArchivedMessages(String jid, int maxResults) {

        MamManager mamManager = MamManager.getInstanceFor(connection);
        try {
            DataForm form = new DataForm(DataForm.Type.submit);
            FormField field = new FormField(FormField.FORM_TYPE);
            field.setType(FormField.Type.hidden);
            field.addValue(MamElements.NAMESPACE);
            form.addField(field);

            FormField formField = new FormField("with");
            formField.addValue(jid);
            form.addField(formField);

            // "" empty string for before
            RSMSet rsmSet = new RSMSet(maxResults, "", RSMSet.PageDirection.before);
            MamManager.MamQueryResult mamQueryResult = mamManager.page(form, rsmSet);

            return mamQueryResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
Malcolm answered 21/1, 2017 at 9:12 Comment(2)
I am not able to find MamManager class. I have added smack 4.2.0. do I need to add openfire also? But I think they have added the support for XEP-313 in smack 4.2.0.Racine
They have added in the experimental repository. My bad.Racine
I
2

The solution you're looking for come under XMPP specification's XEP-0136 Message archiving but Smack has not implemented this features yet. but you can retrieve the message history from server using "custom-stanza" features provided by SMACK API. The following links describe how to send the custom stanza. "How retrieve Chat History using Java Smack library from openfire server?".

Intradermal answered 23/6, 2015 at 9:42 Comment(4)
Can you provide the links you were talking about in your answer, but forgot to actually write? Right now you've just included the link to this SA thread itself. Thanks!Rambutan
your link is broken. It just takes you back to this page.Sesqui
THe Link is brokenClathrate
community.igniterealtime.org/message/249993#249993.. Kindly refer this link... Or you can elaborate here the problem you 're facing..Intradermal
C
1

Finally I got the answer. Archive Messaging features are currently not implemented in Smack library.

https://community.igniterealtime.org/message/249993#249993

Ciri answered 23/6, 2015 at 9:18 Comment(6)
hi Dhiren Hamal, how to use XEP-0136 Archive Messaging to get the chat history in my android application .Spleen
@Spleen ..Did u get any solution to get the chat history?. Please let me know .. Thanks :)Lucullus
@DhirenHamal .. Did u get any solution to get the chat history?. Please let me know .. Thanks :)Lucullus
@Dhiren Hamal .. i didnt found the solution yet.. still searching :(Spleen
@Spleen you can use Open Archive plugin into your openfire server which automatically archive the messages between users conversation and after that you've to pull that stores messages from your client site. Meant to say that I've used spring web server to retrive offline messages.Ciri
@DhirenHamal Can you help with some code snippet. I am using smack api to implement the same.Wendy
F
1

Maybe I am late to answer this question, but may be it will be helpful for others.

get function result of getArchivedMessages(jid, maxResults); from https://mcmap.net/q/1281611/-how-retrieve-chat-history-using-java-smack-library-from-openfire-server

public List<ChatMessage> getChatHistoryWithJID(String jid, int maxResults) {
    List<ChatMessage> chatMessageList = new ArrayList<>();
    MamManager.MamQueryResult mamQueryResult = getArchivedMessages(jid, maxResults);
    String userSendTo = XmppUtils.parseNameFromJID(jid);

    try {
        if (mamQueryResult != null && userSendTo != null) {
            for (Forwarded forwarded : mamQueryResult.forwardedMessages) {
                if (forwarded.getForwardedStanza() instanceof Message) {
                    Message msg = (Message) forwarded.getForwardedStanza();
                    Log.d(TAG, "onCreate: " + msg.toString());
                    Log.d(TAG, "processStanza: " + msg.getFrom() + " Say:" + msg.getBody() + " String length:" + (msg.getBody() != null ? msg.getBody().length() : ""));
                    ChatMessage chatMessage;
                    if (XmppUtils.parseNameFromJID(msg.getFrom().toString()).equalsIgnoreCase(userSendTo)) {
                        chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.RECEIVED);
                    } else {
                        chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.SENT);
                    }
                    chatMessageList.add(chatMessage);

                }
            }
        } else {
            return chatMessageList;
        }

        return chatMessageList;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return chatMessageList;
}

Now the Query will become like:

<iq  id='ri7F7-270' type='set'>
<query  xmlns='urn:xmpp:mam:1' queryid='afd9c922-21cb-437e-b5c4-3a5bf9994e40'>
    <x  xmlns='jabber:x:data' type='submit'>
        <field  var='FORM_TYPE' type='hidden'>
            <value>urn:xmpp:mam:1</value>
        </field>
        <field  var='with'>
            <value>vishal@jabberid</value>
        </field>
    </x>
    <set  xmlns='http://jabber.org/protocol/rsm'>
        <before>
        </before>
        <max>100</max>
    </set>
</query>

And the response will be like:

<message to="vishal@jabberid">
<result xmlns="urn:xmpp:mam:1"  queryid="afd9c922-21cb-437e-b5c4-3a5bf9994e40" id="992">
<forwarded xmlns="urn:xmpp:forward:0">
  <delay xmlns="urn:xmpp:delay" stamp="2019-04-05T06:38:40.612Z"/>
  <message xmlns="jabber:client" to="vishal@jabberid" id="h58k4-104" type="chat" from="vishal@jabberid">
    <body>Hi</body>
  </message>
</forwarded>
</result>
</message>

And more you can be read from this link https://xmpp.org/extensions/xep-0313.html

Ferous answered 8/4, 2019 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.