Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > BOOK: Professional XMPP Programming with JavaScript and jQuery
|
BOOK: Professional XMPP Programming with JavaScript and jQuery
This is the forum to discuss the Wrox book Professional XMPP Programming with JavaScript and jQuery by Jack Moffitt; ISBN: 978-0-470-54071-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional XMPP Programming with JavaScript and jQuery section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2013, 01:52 AM
Authorized User
 
Join Date: May 2013
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't figure out how to handle incoming message stanza

Hello people,

my attempt to create and handle my first message stanza was futile I'm not sure if its the add handler method that I didn't get right or the ping function or the pong function. Basically, I successfully established a connection to xmpp by sending an iq stanza just the way it is done in chapter 3 of the book. Then I created the following handler and functions to broadcast a simple message on all devices connected through my bare jid

Code:
$(document).ready(function(){

    $("#some_button").click(function(){


	        //Add all handlers that will receive ping stanzas at this stage before calling the pings.  I won't include the connection function to keep things simple.  It is the same as in the book, and My_ns is simply my namespace
   
	        My_ns.connection.addHandler(My_ns.get_message, null, "message", "normal", "send_details_id");//Add handler for receiving new message


		$.ajax({//This ajax call retrieves some data from database.  Upon success, the ping stanza is sent 

			type: "POST",

			url: "url.php",

			data: {data1:value1},

			success: function(data) {

			       //Send a message stanza to bare jid, expressing your message
                               My_ns.send_message(My_ns.bare_jid);
	                   					              
                        }

                 });//End ajax

    	
    });//End click function


     //Now define the get_message(pong) and send_message(ping) functions

    	My_ns.send_message = function (to) {

                var send_details = $msg({

            	to: to,

            	type: "normal",

            	id: "send_details_id"}).c("body").t("hohoho");


        	alert("Sending message to " + to + ".");

        	My_ns.connection.send(send_details);

    	}//End send_message


    	My_ns.get_message = function (message) {

        	var detail = $(message).children('body').text();

		alert(detail);


    	}//End get_message

});//End document ready

Well I expect to get an alert saying "sending message to 'myjid' ", followed by a "hohoho". The ajax successfully gets executed but I don't get my expected alerts, meaning I'm not doing something right with the stanzas. Any help please??
 
Old June 1st, 2013, 12:03 PM
Wrox Author
 
Join Date: Jan 2010
Posts: 178
Thanks: 0
Thanked 16 Times in 15 Posts
Default

A few issues:

1) You don't have an error handler set for your ajax call. It might be failing and not calling the code to send the message at all.

2) Your message handler is not returning true, so it can only be used once.

3) Specifying both type and id is a little much on your handler, but I'm assuming this is a small example out of context.

Your code should work as written, although not returning true from the handler will make it delete the handler when it runs the first time. I think the issue is that your ajax query is failing and the send is never being triggered.

Try putting logging in for an implementation of rawInput and rawOutput so that you can see what traffic is being sent and received.
 
Old June 1st, 2013, 03:30 PM
Authorized User
 
Join Date: May 2013
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by metajack View Post
A few issues:

1) You don't have an error handler set for your ajax call. It might be failing and not calling the code to send the message at all.

2) Your message handler is not returning true, so it can only be used once.

3) Specifying both type and id is a little much on your handler, but I'm assuming this is a small example out of context.

Your code should work as written, although not returning true from the handler will make it delete the handler when it runs the first time. I think the issue is that your ajax query is failing and the send is never being triggered.

Try putting logging in for an implementation of rawInput and rawOutput so that you can see what traffic is being sent and received.

Thanks for that input metajack. My Ajax works fine. It actually makes the required changes to the database. I just omitted the error handler to keep things simple. So I'm assuming that issue number two is where the problem is arising. I'm going to look into that right now.
 
Old June 1st, 2013, 04:35 PM
Authorized User
 
Join Date: May 2013
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by metajack View Post
A few issues:

1) You don't have an error handler set for your ajax call. It might be failing and not calling the code to send the message at all.

2) Your message handler is not returning true, so it can only be used once.

3) Specifying both type and id is a little much on your handler, but I'm assuming this is a small example out of context.

Your code should work as written, although not returning true from the handler will make it delete the handler when it runs the first time. I think the issue is that your ajax query is failing and the send is never being triggered.

Try putting logging in for an implementation of rawInput and rawOutput so that you can see what traffic is being sent and received.


Well I made two changes and it now works. First I made sure the handler returns true but more importantly, I sent the message to the full jid and not the bare jid. Which raises a new question. Aren't stanzas sent to a bare jid supposed to be received by all devices connected via that bare jid? I thought that was the case.
 
Old June 2nd, 2013, 01:07 AM
Wrox Author
 
Join Date: Jan 2010
Posts: 178
Thanks: 0
Thanked 16 Times in 15 Posts
Default

The bare jid will deliver to all *available* jids. Did you send available presence for the jid you want to receive the message? If not, the server will store it in offline cache until you send available presence.
 
Old June 2nd, 2013, 05:48 PM
Authorized User
 
Join Date: May 2013
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by metajack View Post
The bare jid will deliver to all *available* jids. Did you send available presence for the jid you want to receive the message? If not, the server will store it in offline cache until you send available presence.
Ok I think that's the problem. I didn't send presence. So let's get this clear, if I send presence to any resource, that resource would automatically receive incoming message stanzas addressed to the bare jid right?
 
Old June 3rd, 2013, 01:14 AM
Wrox Author
 
Join Date: Jan 2010
Posts: 178
Thanks: 0
Thanked 16 Times in 15 Posts
Default

When you connect to an XMPP server, you are initially unavailable. You must send available presence to go online visible to your contacts.

When a message is received to a bare JID, teh server delivers it to all available resources; if no resources are available, it puts it in offline storage and will deliver it as soon as a resource comes online. If it's sent to a full JID it delivers it straight to the resource if that resource is available or returns an error if not.

It is not controlled by someone else sending presence to you, but you sending your own presence of unavailable or available.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Send Incoming Email Edward Colins BOOK: Beginning SharePoint 2010 Administration: Windows SharePoint Foundation 2010 and Microsoft Sha 1 October 4th, 2010 08:33 AM
Handling incoming call Nimeshu .NET Framework 1.x 1 August 23rd, 2007 09:31 AM
Can I emit a tag using an incoming parameter? dmorin XSLT 3 August 14th, 2006 09:03 AM
Incoming Email handling BaNSHee SQL Server ASP 0 April 6th, 2005 05:00 AM
How to Keep track of all incoming Documents reddynet Biztalk 0 July 17th, 2003 04:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.