Using JAXL with Google Cloud Messaging

838 views
Skip to first unread message

Ben Harold

unread,
Dec 30, 2013, 5:17:13 PM12/30/13
to ja...@googlegroups.com
Hi,

I'm attempting to use JAXL to run an XMPP server to communicate with the Google Cloud Messaging service. I followed the xmpp_reset.php example from GitHub. This is what my server "run" method looks like:

 public function xmpp()
 
{
     
// The XMPP connection requires a sender ID (project number) and an API key
     $senderId
= \Config::get('gcm.senderId');
     $apiKey  
= \Config::get('gcm.apiKeyDebug');

     
// initialize xmpp client
     $xmpp
= new \JAXL(array(
         
'jid'       => $senderId . '@gcm.googleapis.com',
         
'pass'      => $apiKey,
         
'auth_type' => 'PLAIN',
         
'host'      => 'gcm.googleapis.com',
         
'port'      => '5235',
         
'force_tls' => true,
         
'log_level' => JAXL_INFO
     
));

     
// register callbacks on required xmpp events
     $xmpp
->add_cb('on_auth_success', function() {
         
global $xmpp;
         _info
("got on_auth_success cb, jid ".$xmpp->full_jid->to_string());
     
});

     require_once JAXL_CWD
.'/http/http_server.php';
     $http
= new \HTTPServer();

     
// add generic callback
     $http
->cb = function($request) {
         
// For demo purposes we simply return xmpp client full jid
         
global $xmpp;
         $request
->ok($xmpp->full_jid->to_string());
     
};

     
// This will start main JAXLLoop, hence we don't need to call $http->start() explicitly
     $xmpp
->start();
 
}

When I "run" my server, this is the output:

jaxl:222 - 2013-12-30 21:57:42 - strict mode enabled, adding exception handlers. Set 'strict'=>TRUE inside JAXL config to disable this
jaxl
:167 - 2013-12-30 21:57:42 - created pid file /Library/WebServer/Documents/firestone/fuel/app/vendor/abhinavsingh/jaxl/.jaxl/run/jaxl_30090.pid
jaxl_socket_server
:63 - 2013-12-30 21:57:42 - socket ready to accept on path tcp://127.0.0.1:9699
jaxl_socket_client
:95 - 2013-12-30 21:57:42 - trying tcp://gcm.googleapis.com:5235
jaxl_socket_client
:175 - 2013-12-30 21:57:42 - socket eof, disconnecting


It looks like I'm able to connect to the googleapis.com server, but then it immediately disconnects without authenticating. At that point, the script will just sit there until I hit control-C. When I press control-C, this is the output:

^Cjaxl:779 - 2013-12-30 22:06:11 - event 'end_stream' catched in handle_other with stanza name
xmpp_stream
:112 - 2013-12-30 22:06:11 - got invalid return value from state handler 'wait_for_stream_start', sending end stream...
xmpp_stream
:115 - 2013-12-30 22:06:11 - state handler 'logged_out' returned N;, kindly report this to developers
jaxl_exception
:48 - 2013-12-30 22:06:11 - got jaxl exception construct with fclose() expects parameter 1 to be resource, null given, 2, /Library/WebServer/Documents/firestone/fuel/app/vendor/abhinavsingh/jaxl/core/jaxl_socket_client.php, 126
jaxl
:214 - 2013-12-30 22:06:11 - cleaning up pid and unix sock files
jaxl_socket_server
:75 - 2013-12-30 22:06:11 - shutting down socket server
jaxl_clock
:55 - 2013-12-30 22:06:11 - shutting down clock server...


So, it appears that I can connect, but I'm not authenticating for some reason. Why might that be?

Also, once I've established the server connection, how do I send new messages? Is there a run loop that I can access? I need to check a database for messages every five minutes, sending new messages as they arrive. In other daemon-type scripts in the past I've typically used something like:

while ($server->run())
{
    // do whatever the server is supposed to do
    $messages = Messages::new();
   
foreach ($messages as $message)
   
{
        $server
->send($message);
   
}
   
    sleep
(5 * 60) // wait 5 minutes before you do it again
}


How does the loop work in JAXL? How do I get inside of it? How do I specify how long between iterations?

One last thing: what's with port 9699? Does that need to be opened?

Thanks,

Ben

Ben Harold

unread,
Jan 3, 2014, 10:14:23 PM1/3/14
to ja...@googlegroups.com
Oh man, that's totally crazy! I'm having the exact same problem! </shamelessBump>

MDKMarshal

unread,
Apr 8, 2014, 7:03:42 AM4/8/14
to ja...@googlegroups.com
I'm currently trying to get GCM working with JAXL, and I've come across this error myself. The root cause of this seems to be an overly simplistic implementation of how JAXL uses SSL. Note that JAXL is trying to open a connection like this:

jaxl_socket_client:95 - 2013-12-30 21:57:42 - trying tcp://gcm.googleapis.com:5235

This looks all well and good, except that GCM requires a TLS connection (see: http://developer.android.com/google/gcm/ccs.html). We're not actually attempting to create a secure connection, so Google's server is basically telling us to take a running jump. An error message of some kind would be great, but we get what we get.

If you force JAXL to connect via SSL, you end up with the following:

jaxl:238 - 2014-04-08 10:53:24 - strict mode enabled, adding exception handlers. Set 'strict'=>TRUE inside JAXL config to disable this
jaxl:188 - 2014-04-08 10:53:24 - created pid file lib/JAXL-3.0.0/priv_dir/run/jaxl_11263.pid
jaxl_socket_client:95 - 2014-04-08 10:53:24 - trying ssl://gcm.googleapis.com:5235
jaxl:230 - 2014-04-08 10:53:24 - cleaning up pid and unix sock files
jaxl_clock:55 - 2014-04-08 10:53:24 - shutting down clock server...

Note that the only thing I'm doing is creating a JAXL instance and running the start() - there's nothing else in the script, so it just ends, calling the JAXL deconstructor and cleaning up pid's and sock files.

This is all well and good, if it weren't for the method that JAXL uses to determine if it wants to open a vanilla TCP connection or use SSL. The method that does this is JAXL->get_socket_path(), which looks like this:

public function get_socket_path() {
return ($this->cfg['port'] == 5223 ? "ssl" : "tcp")."://".$this->cfg['host'].":".$this->cfg['port'];
}

This builds the socket path that it will try to open a connection to, including the protocol. I'm new to XMPP, so I can only assume that port 5223 is the default 'secure' port. Because of this, we are able to open secure connections if and only if we're connecting over port 5223, which we're not.

I think that this function should respect the 'force_tls' configuration parameter, and force us onto SSL, though I'm not 100% sure of the repercussions of this throughout the rest of the JAXL project.
Reply all
Reply to author
Forward
0 new messages