Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to add a chat to my web application written in Java.

What would be the best solution in this matter? I have read something about JMS, but the pub/sub pattern doesn't seem to be designed for this kind of usage, and for the p2p pattern, I need to build a queue for each user(this doesn't sound right neither).

What would you suggest?

share|improve this question

closed as too broad by MichaelT, Ixrec, gnat, Snowman, Bart van Ingen Schenau Feb 1 at 9:57

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

Just throwing out a suggestion here: Look at Spring Integration… – smp7d Mar 13 '12 at 14:55
up vote 1 down vote accepted

In web programming, there is an upcoming alternative to the "ping" based approach, known as WebSockets. It is supported by Chrome as of version 14, Firefox as of version 6, and by the IE 10 developer preview.

If this is for a personal or student project, I'd highly recommend giving it a try, as using web sockets may become important in the future, and understanding how to use them may be something that would help you land a job at an interview.

If it's for production code, since web sockets are still fairly new and not supported by all browsers (an older version of the standard has been around for years, but was found to have security flaws, a newer version addressing those flaws came out in the fall), it may not be an option, unless you know your clients all use recent versions Chrome or Firefox.

share|improve this answer

You could choose one of the many open source Java chat servers from this list.

share|improve this answer
+1 - this is one place where you do not want to reinvent the wheel. – user1249 Mar 14 '12 at 10:20

If it's a web chat I would go with a "ping" style approach with javascript. Every N seconds the javascript will ping the server looking for any messages in the queue. Look into javascript's XMLHttpRequest object.

The advantage of a "ping" style is it fits within the web-based "request" model. The client (browser) "pulls" from the server instead of the server taking the initiative and "pushing" data to the client. You don't have to install any client side software. It's just a bunch of regular old get and put requests.

The disadvantage of "ping" is it's wasteful. Lots of needless pings when ideally the server should be able to "push" to the client when there is a message. Also it may appear slower. You won't see a message until the next ping which could be a few seconds.

You could use a java applet but that would be "client side" software. It's no longer a pure "web based" solution and many users will walk away if they need to install some sort of applet plugin. Same goes for flash/silverlight.

share|improve this answer
"Ping" style doesn't need to be wasteful, nor does it have to impose extra waiting. An asynchronous request will tell you when there's data available. Since it is asynchronous, you do do the next call immediately after processing the current one. – Jerry Coffin Mar 13 '12 at 19:23
@JerryCoffin It depends on what your definition of wasteful is. Ping style forces you to go to the server even when there are no messages. That's waste. (although not a big deal). – Lord Tydus Mar 13 '12 at 21:56
Perhaps I should have said "even close to as wasteful, or in the same way(s)." – Jerry Coffin Mar 13 '12 at 22:20
@myself. Wasted pings can be a big deal if on a mobile device. It takes battery power to transmit messages. Hundreds of wasted pings over a chat session will add up. – Lord Tydus Mar 13 '12 at 22:36
Sending lots of pings is wasteful -- sending a single ping, then getting a reply when there's something to download, much less so, even at worst. – Jerry Coffin Mar 13 '12 at 23:34

If you need to write it by hand and from the scratch, then I suggest to have a look at Netty.

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.

If your "hacker sense" tickles, then have a look at Node.js

share|improve this answer

Take a look at Atmosphere

It offers a very comprehensive approach to the problem that is compatible with a wide range of clients, down to IE5. It can be configured to work within SpringMVC and will typically require a WebSockets enabled server - such as Jetty 7 (which is now operated by the Eclipse Foundation). It's free and open source.

There are many examples of different use cases (e.g. jQuery interface, guice implementation, meteor chat and so on), so you should get a basic project off the ground in no time at all.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.