powered by Jive Software

Smack message Receive quest

Hi,

Sorry I may take ur some time.

I am new jabber developer and i am using smack-1.0-beta3.I have some difficulties handling Chat objects.

Suppose I make a chat session program (Client 1) where I open an XMPPConnection & accept username, password, resource and jabberserver address. Thru this connection I send available type presence packet. I Create a

Chat chat = new Chat(FriendJID).

Now another jabber client IM say EXODUS logged as my FriendJID having my JID in its roster; will recognize me online. Then I dochat.sendMessage(“Hello”) ;

and EXODUS receives my message. This all goes perfectly fine.

Now suppose I write another program (Client 2) for receiving message from Client 1. I donot receive any.

Client 1.java:

/* User: suraj@jabber.org/test mailto:suraj@jabber.org/test */

XMPPConnection con = new XMPPConnection(“jabber.org”);

con.login(“suraj”, “secret”,“test”);

/********/ Chat chat = con.createChat("vinit@jabber.org");

chat.sendMessage(“Hey, how’'s it going?”);

Client 2.java:

/* User: vinit@jabber.org/test mailto:vinit@jabber.org/test */

XMPPConnection connection = new XMPPConnection(“jabber.org”,5222);

connection.login(“vinit”, “secret”,“test”);

/**********/ Chat chat = connection.createChat("suraj@jabber.org");

while(true)

{

Message msg = chat.pollMessage();

if(msg!=null) {msg1 = msg.getBody();

System.out.println("Msg From Suraj: "+msg1); break;}

try{Thread.sleep(10);} catch(InterruptedException e) {}

}

DIFFICULTY FACED:

  1. There is no synchro between when Client 1 & client 2 chat sessions. Because when Client 1 opens chat with (suraj@jabber.org) participant, It may happen that suraj is not online or he is just online but has not opened his chat session with vinit@jabber.org participant & vice versa.

See the statements preceeded by comments /********/

how can they execute at a same time? No guarentee.

So one of the 2 clients will always be unable to keep track of the other’‘s chat session whether it has been opened by opposite client or not.2. May be some chat_id conflict. may be both are not the same and hence they don’'t go at proper sessions. But how come a normal EXODUS type messenger keeps track of any chat when it is created by client 1 program. Thats because EXO must be polling all time to receive any

new chat messages or request. But how does it do so?

  1. One could expect a client to keep polling after it has just authenticated.This polling will let that client receive message from other party and then he/she can know and begin chat with that corresponding participant with unique chat_id and next time all messages would go to proper chat session using that chat_id.

But polling is possible only if u open a chatobject. How can a client poll on an unkown recipient chat object everytime. Smack doesn’'t support any chatobject with unknown or temporary JID that would be used for

polling.

In short messaging between 2 smack programs could not be done but messaging between smack program and EXODUS is smooth and as expected.

Another important thing: If I integrate Client1 and Client 2 programs in single chat program, and try sending message to myself

{

XMPPConnection connection = new XMPPConnection(“jabber.org”);

connection.login(vinit,secret);

chat.sendMessage("vinit@jabber.org");

while (true)

{

Message msg = chat.pollMessage();

if(msg!=null)

{Display(msg.getBody);}

try{Thread.sleep();} catch(InterruptedException e) {}

}

}

THEN I RECEIVE THE MESSAGE. (Strange !)

Why is this problem.

Thanking you,

vinsan

Smack uses a thread id to determine what messages belong in a chat. When you use Connection.createChat() you are essentially creating a ‘‘channel’’ tuned to messages that are sent by the person you specify AND that use the thread ID Smack generates at that point. So your two client example fails because each client is creating a different channel (each call is tuned to the correct user but generates a unique thread id and so won’'t show messages outside that thread). You can easily see this if you turn the debug window on in Smack when you run this example. The messages are being sent.

Since your second example uses the same Chat object, you’‘re using the same thread ID to send and receive. Thus you’'re tuned into the same channel and receive the messages correctly.

To make your example work, you need to listen for message packets coming into the second chat client. When you receive one, create a new Chat object with that thread ID (see constructors for the Chat class):

Chat newChat = new Chat(conn,user,threadid);

-iain

Message was edited by: iain