send and receiving message using smack API

19,642

Solution 1

I had a similar problem, after following the tutorial here (http://www.javacodegeeks.com/2010/09/xmpp-im-with-smack-for-java.html) and this is what I found:

When you create the chat, you chat the user you want to connect to (eg in my case "user1@gbd038"). This works fine if user1 is using a GUI client such as Spark (which presumably has built-in support and/or error handling for this), and user1 will receive the message. This process attaches the messageListener to a chat now associated with "user1@gbd038". However, when I reply from Spark as user1, the chat that smack receives is actually coming through complete with the location tag, eg:

Received message 'hi' from user1@gbd038/Spark 2.6.3

So it creates a new chat that the application is not listening for (and therefore your application will not receive / print out). I have found two ways to solve this problem:

  1. use the location tag when starting the conversation (although this doesn't seem very scalable or robust):

    xmppManager.sendMessage("Hello mate", "user1@gbd038/Spark 2.6.3");

  2. as Robin suggested, use a ChatManagerListener (which will create a new chat when receiving the reply from user1, which can be forwarded to the messageListener):

    chatManager = connection.getChatManager();
    messageListener = new MyMessageListener();
    
    chatManagerListener = new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean createdLocally) {
            chat.addMessageListener(messageListener);
        }
    };
    chatManager.addChatListener(chatManagerListener);
    

Hope that helps someone in the same position!

Solution 2

You are creating a chat and sending a chat message from both ends but not listening for a chat from either. Use a ChatManagerListener to listen for incoming chats from other clients.

Solution 3

Use below code for sending and receiving message

@Override
         public void processMessage(Chat chat, Message message) {
             // Print out any messages we get back to standard out.
             System.out.println("Received message: " + message);
         }

     });

chat.sendMessage("How are you dear !!");
System.out.println(" Send Message succesfully");

For full code example visit How to send message using smack api

Share:
19,642
Sameek Mishra
Author by

Sameek Mishra

Updated on June 05, 2022

Comments

  • Sameek Mishra
    Sameek Mishra about 2 years

    I have setup my open fire(jabber server) on local machine with two user testuser1 and testuser2 .using Spark client both users perform chat without any issue,it's nice.

    openfire IP -192.168.1.65

    I want to use smack API(3.3.0) for send and receiving message. i have write sender side code to send message(with testuser1) and tested with Spark client(with testuser2) message received on testuser2 side,but when i try with java code to receive sender message ,i am not able to receive those publish messages.

    Sender.java

    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.MessageListener;
    
    public class Sender 
    {
    
        public static void main(String a[]) throws XMPPException, InterruptedException
        {
             XMPPConnection connection = new XMPPConnection("192.168.1.65");  
             System.out.println(connection);
             connection.connect();
             connection.login("testuser1", "test123");
    
    
    
             Chat chat = connection.getChatManager().createChat("testuser2@sameek", new MessageListener() {
    
                 public void processMessage(Chat chat, Message message) {
                     // Print out any messages we get back to standard out.
                     System.out.println("Received message: " + message);
                 }
             });
             chat.sendMessage("Howdy test1!");
    
             while (true) {
            Thread.sleep(50);
        }
    
    
    
    
    
        }
    
    }
    

    Receiver.java

      import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.MessageListener;
    
    
    
    
    
    
    public class Receiver
    {
    
        public static void main(String a[]) throws XMPPException,, InterruptedException
        {
             XMPPConnection connection = new XMPPConnection("192.168.1.65");  
             System.out.println(connection);
             connection.connect();
             connection.login("testuser2", "test123");
    
    
    
             Chat chat = connection.getChatManager().createChat("testuser1@sameek", new MessageListener() {
    
                 public void processMessage(Chat chat, Message message) {
                     // Print out any messages we get back to standard out.
                     System.out.println("Received message: " + message);
                 }
             });
             chat.sendMessage("Howdy test2!");
    
             while (true) {
            Thread.sleep(50);
        }
    
    
    
    
    
        }
    
    }
    

    please help me and suggest if i am following wrong approach.

    Thanks