Hey guys,

I am doing a very simple POC regarding Instant Messaging using Struts.
So I was able to do login and sending message. However, I am confused on how I can get the messages display to my POC if the other person is replying.

The idea that I am thinking is to use AJAX periodically call the Struts action.
Can you advise on what API i should use in AJAX to do this call?

Regards,

Recommended Answers

All 5 Replies

What is POC?

What is POC?

Hi,

POC = Proof of Concept

3vilwyatt,

Unless you're going to invest time in writing an applet, then yes, you are forced to into "pulling" data from the server (with AJAX) as data cannot be "pushed" by the server into javascript/browser.

In a project of this size, you will save lots of time (and lots of code) by using a factory lib such as jQuery or Prototype, even if this means leaning the lib as you go.

Here's the sort of pattern you might consider, at least to get you started (untested):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Airshow :: Untitled</title>
<style type="text/css">
button { margin-right:5px; }
h1 {  }
h2 { margin: 0; }
#input, #controls { margin-top: 10px; }
#im_conversation {
	width: 300px;
	height: 300px;
	overflow: auto;
	border: 1px solid #999;
}
</style>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
	var interval = 3000;//milliseconds
	var allowGetLatest;//flag that allows/disallows im_getLatest cycle
	function im_getLatest() {
		var args = "xxxxx";//here compose string of arguments, as required server-side by getLatest.php.
		$.ajax({
			type: "GET",
			url: "getLatest.php?" + args,
			success: function(data, textStatus, XMLHttpRequest) {
				//here: put data into #im_conversation
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				//here: handle error cases (if necessary)
			},
			complete: function(XMLHttpRequest, textStatus){//this fires after success or error
				if(allowGetLatest) {
					setTimeout(im_getLatest, interval);//Delay then call im_getLatest again.
				}
			}
		});
	}
	$("#im_start").click(function(){//simple manual start
		allowGetLatest = true;
		im_getLatest();
	});
	$("#im_stop").click(function(){//simple manual stop
		allowGetLatest = false;
	});
});
</script>
</head>

<body>

<h1>Instant Messenger POC</h1>
<h2>Conversation</h2>
<div id="im_conversation"></div>

<div id="input">
	<input type="text" size="50" />
</div>

<div id="controls">
	<!-- These controls demonstrate that the im_getLatest process can be started and stopped -->
	<button id="im_start">Start</button>
	<button id="im_stop">Stop</button>
</div>

</body>
</html>

Airshow

Hi Airshow,

Thanks for your response!
Now that you mentioned it, I actually have the Prototype.js from some online project i saw.
This is the code snippet. I was thinking if it is better to use Struts or just a simple servlet instead. I am using JAVA btw.

updater = new Ajax.PeriodicalUpdater('currentChat', 'ChatServlet',
                {
                    method: 'get',
                    insertion: Insertion.Bottom,
                    frequency: 3,
                    parameters: {getMessages: targetUser}
                });

Framework-wise, is it possible to combine - AJAX, Struts to do this?
I was also thinking of using EIMP to connect to Yahoo, Gtalk, ICQ and AIM.

Sorry if my questions are more on brainstorming. I am still on the verge of putting all my ideas together.

Regards,

3vilwyatt,

I worked on a Struts site once and as far as I was concerned it was an unnecessary piece of black magic designed to confuse developers. As far as I could make out from Apache's documentation, Struts is a server-side thing that sorts out url-to-source-file mapping (and mime types?), ie. a more readily controllable version of what can be done with MOD-Rewrite alone.

As far as the client is concerned, the browser simply makes HTTP requests by URL in the normal way, unaware of the Struts layer (or anything else server-side for that matter).

Since AJAX is nothing more than a way of issuing HTTP requests and handling the response in javascript, it can certainly be combined with Struts (at least if my understanding of Struts is correct).

Similarly, Struts is not mutually exclusive with jQuery/Prototype, both of which are client-side (javascript) productivity libs. The big advantage of jQuery over Prototype is the amount of support available (on Daniweb and elsewhere).

Maybe someone else can help with EIMP.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.