Should I be using threads for creating connections/chatting in my program?

I need to write a basic chat program, and I created the GUI using JFrame. Should I be using a Thread to create the connection, or request chats? Is there an example that I can follow. Basically I am having problems requesting chats with other people, and in a previous thread it was mentioned that I should be using threads.

And by threads, I am assuming its a separate class that is called and running as a separate process?

You don’'t necessarily need to use threads.

I have one app where chatting is its secondary responsibility so I spawn a thread off that handles all connectivity and communication with the chat server as I don’'t want any failures to interfere with the primary responsibility of the process.

On the other hand I have another app that absolutely depends on having a connection to the chat server so in this app I don’'t spawn a thread and instead wait for connectivity on the main thread.

As far as a Swing app if you don’'t want to block the event dispatcher thread (thereby making your GUI unresponsive) while you establish a connection you may want to spawn off the establishing of connectivity to a thread.

So the answer really depends on your requirements.

Hi Phil,

if you don’'t want to use threads you may also register an OnMessage method, so Smack will create the thread for you.

LG

Where is the OnMessage method?

Sorry, i’'ve only used threads once and it was a while ago. How do I access the connection variable initialized in the connectio thread in my chat program? So…after I obtain a connection on my thread class, how do I refer to the Connection class variable created, in my chat program (i.e. for creating chats, etc.?)

The usage of threads would imo be entirely dependent on your use case.

If you were envisaging lots of conversations happening at once then you may want to have each conversation running on a separate thread. If you only expect a couple of conversations happening at once then it may not be worth the extra complexity adding thread support to your application.

You mentioned that you were having problems requesting chats with other people, but you didn’t actually say what these problems were. If you could provide a bit more information I’'ll try and give you some pointers

Jon

^^ Here is a link to the original problem:

http://www.jivesoftware.org/community/thread.jspa?threadID=19048&tstart=0

Please feel free to respond here though. I greatly appreciate your time.

LG,

could you elaborate on the OnMessage event that you speak of?

I searched the javadocs and could not find this method.

Thanks!!

As far as I know there is not such OnMessage method. I imagine LG was pertaining to a Listener listening out for message events.

I could be wrong though

As someone has said before, using chats would depend on your situation. But from my experience (i wrote a jabber client whose main functionality was to chat).

Create a thread when you connect to the jabber server, login to is and retrieve the roster

It maybe good to create a thread when you are retrieving a VCard

You do not need to creat a thread to create a Chat object and send messages through it

Usually, you will prefer to handle the incoming messages in an asynchronous way, by using listeners, so no thread is needed for the incmoming messages.

/html

To create a thread in your case (this is just an example)

public class YourApp extends JFrame

{

XMPPConnection connection;

public YourApp()

{

//Create GUI

}

private class YourThread extends Thread

{

YourThread()

{

start();

}

public void run()

{

//All the variables are accessible here

//Connect to the server

//Log to the server

//Get your roster

//Make your contact tree…

}

}

}

/code