Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > BOOK: Professional XMPP Programming with JavaScript and jQuery
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 1st, 2013, 01:52 AM
Authorized User
Points: 182, Level: 3
Points: 182, Level: 3 Points: 182, Level: 3 Points: 182, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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??
  #2 (permalink)  
Old June 1st, 2013, 12:03 PM
Wrox Author
Points: 702, Level: 10
Points: 702, Level: 10 Points: 702, Level: 10 Points: 702, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
  #3 (permalink)  
Old June 1st, 2013, 03:30 PM
Authorized User
Points: 182, Level: 3
Points: 182, Level: 3 Points: 182, Level: 3 Points: 182, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
  #4 (permalink)  
Old June 1st, 2013, 04:35 PM
Authorized User
Points: 182, Level: 3
Points: 182, Level: 3 Points: 182, Level: 3 Points: 182, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
  #5 (permalink)  
Old June 2nd, 2013, 01:07 AM
Wrox Author
Points: 702, Level: 10
Points: 702, Level: 10 Points: 702, Level: 10 Points: 702, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
  #6 (permalink)  
Old June 2nd, 2013, 05:48 PM
Authorized User
Points: 182, Level: 3
Points: 182, Level: 3 Points: 182, Level: 3 Points: 182, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
  #7 (permalink)  
Old June 3rd, 2013, 01:14 AM
Wrox Author
Points: 702, Level: 10
Points: 702, Level: 10 Points: 702, Level: 10 Points: 702, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:36 AM.


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