powered by Jive Software

Receiving chat messages


I want to manage Chat, I wonder about the difference of two methods to get Chat messages from others:


Chat newChat = connection.createChat("jsmith@jivesoftware.com");

Message message = newChat.nextMessage();


Use a RosterListener

I use case (2), wich is fine, but I noticed there’'s also method (1), wich seems to have the advantage to “keep” messages


Dear Ofupien

Please elobrate on what “keep” messages mean ? Don’'t you think that Chat.nextMessage() is blocking when you could use a PacketListener instead ?


Hi Rajesh,

thanks, I do use now a PacketListener, this works great, but my question was just because I noticed on the example at:


showing a chat example using

Message message = newChat.nextMessage();

And I wondered how this worked, why the “nextMessage()”.


Dear Ofupien

If you are writing code where you are going to pool for messsages i.e. wait for messages and do something with them then you could use the Chat.nextMessage(). On the other hand if you want your code to respond in the event of an appropriate message arriving you have to go with a PacketListener.

I am sure that you would want your code to do something or be idle till a packet arrives instead of blocking waiting for a packet to arrive.

You could even look at the PacketCollector in case you HAVE to wait for a packet to arrive.

I do not know if I am understanding you correctly, but would like to share my thoughts




The Chat class allows you to maintain a chat with another user in an easy way. You could certainly avoid to use the Chat class but you will have to manually take care of many details.

The Chat class provides many ways to get messages from the other participant.


Returns the next available message in the chat. The method call will block (not return) until a message is available.

Chat.nextMessage(long timeout)

Returns the next available message in the chat. The method call will block (not return) until a packet is available or the timeout has elapsed. If the timeout elapses without a result, null will be returned.


Polls for and returns the next message, or null if there isn’'t a message immediately available. The method call will always return immediately, whereas the nextMessage method will return only when a message is available (or after a specific timeout).

Chat.addMessageListener(PacketListener listener)

Adds a packet listener that will be notified every time a new message in the chat arrives.


– Gato

Thanks you Rajesh and Gato for your explanations. I understand the difference now.


For Coldfusion

works as far as a timer goes, but still it seems to fail

to gather incoming packets



What version are you using? If you are filtering by chatID (default behavior) then make sure that you are receiving a message whose “thread” matches the Chat’'s ID.

You can use the debugger window to collect this info.


– Gato

Hello Gato,

How do you set up the debugger in CFMX and if you can’'t do that how can it be done from a java application? I can make .class files and have CFMX use them and submit strings to login but I have not sent a message this way, however CFMX can send messages, the only part remains is incoming messages. I was wondering if I could just save them (the messages) in a database JDBC and use CFMX to read from that datasource. I have the JDBC working in CFMX now I just have to connect the two. Thanks Dan.

I have working apps you can use to find out the remaining

problem, just email me when you are ready. Thanks Dan


Sorry for the late reply but I was away for several days on vacation. Unfortunately I don’'t have any experience with CFMX but you can follow this link http://www.jivesoftware.com/xmpp/smack/documentation/debugging.html to learn how to enable the debugging mode in Smack in a regular java application.

I was wondering if I could just save them (the

messages) in a database JDBC and use CFMX to read

from that datasource.

Why would you like to save the messages in the DB? Do you need to persist them even that you read them? Do you need to query on the received messages? An XMPP server should save the message if the target user is offline so that when the user becomes online he/she could receive any deferred message.

Regarding your initial problem I think that the best way to understand what’'s going on is to open a debugger and collect the XML stanzas that you are receiving. BTW, what Smack version are you using?


– Gato