title: Unable to connect to GTalk with node.js and node-xmpp
body: <p>I am trying to connect to GTalk using node.js and node-xmpp. Instead of successfully connecting, node-xmpp returns a XML error response.</p>
  
  <h2>Preparation</h2>
  
  <pre><code>$node -v
  v0.10.24
  $npm install node-stringprep
  $npm install node-xmpp
  $npm list
  ...
  ├─┬ node-xmpp@0.12.2
  ...
  </code></pre>
  
  <h2>gtalk.js</h2>
  
  <pre><code>var xmpp = require('node-xmpp');
  var sys = require("sys");
  
  var options = {
      jid: "mygoogleid@gmail.com",
      password: "mygooglepassword"
      //,host: "talk.google.com"
      //,port: 5222
  };
  
  var jabber = new xmpp.Client(options);
  
  jabber.on('online', function() {
      console.log("Connected");
  });
  
  jabber.on('error', function(e) {
      sys.puts(e);
  });
  </code></pre>
  
  <h2>Error</h2>
  
  <pre><code>$node gtalk.js
  &lt;stream:error xmlns:stream="http://etherx.jabber.org/streams"&gt;&lt;invalid-xml xmlns="urn:ietf:params:xml:ns:xmpp-streams"/&gt;&lt;/stream:error&gt;
  </code></pre>
  
  <h2>Possible explanations</h2>
  
  <ol>
  <li><del><strong>ID/password is wrong</strong> (login at google website does works)</del></li>
  <li><del><strong>node-xmpp needs host/port to connect to GTalk</strong> (tried talk.google.com/5222)</del></li>
  <li><del><strong>talk.google.com@5222</strong> is wrong (<a href="https://developers.google.com/talk/open_communications?csw=1" rel="nofollow noreferrer" title="google">Google Doc Open Com</a> says it's correct)</del></li>
  <li><del><strong>GTalk only allows OAuth2</strong> (<a href="https://developers.google.com/talk/open_communications?csw=1" rel="nofollow noreferrer" title="google">Google Doc Open Com</a> says SASL PLAIN is allowed for "legacy" devices)</del></li>
  <li><del><strong>This is the same as <a href="https://stackoverflow.com/questions/4349577/connecting-to-google-talk-over-xmpp-on-node-js" title="stackother">StackOverflow#4349577</a></strong> (does not have same typo)</del></li>
  <li><del><strong>I am using node-xmpp wrong</del></strong></li>
  <li><del><strong>GTalk does not respond as expected</del></strong></li>
  <li><strong>node-xmpp is buggy<del>/does not support GTalk</del></strong></li>
  </ol>
  
  <p>I am pretty sure to have 1-5 ruled out and I would be glad to hear other explanations or evidence for 6-8. Thanks!</p>
  
  <h2>Updates</h2>
  
  <p><strong>1. XML Stream ("=>" outbound, "&lt;=" inbound)</strong></p>
  
  <pre><code>=&gt; &lt;stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="gmail.com"&gt;
  &lt;= &lt;stream:stream from="gmail.com" id="C2AA8A4648B596A1" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"&gt;&lt;stream:features&gt;&lt;starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"&gt;&lt;required/&gt;&lt;/starttls&gt;&lt;mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"&gt;&lt;mechanism&gt;X-OAUTH2&lt;/mechanism&gt;&lt;mechanism&gt;X-GOOGLE-TOKEN&lt;/mechanism&gt;&lt;/mechanisms&gt;&lt;/stream:features&gt;
  =&gt; &lt;starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/&gt;
  &lt;= &lt;proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/&gt;
  =&gt; &lt;stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="gmail.com"&gt;
  &lt;= &lt;stream:stream from="gmail.com" id="3D42C6DC5985A9E6" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"&gt;
  &lt;= &lt;stream:features&gt;&lt;mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"&gt;&lt;mechanism&gt;X-OAUTH2&lt;/mechanism&gt;&lt;mechanism&gt;X-GOOGLE-TOKEN&lt;/mechanism&gt;&lt;mechanism&gt;PLAIN&lt;/mechanism&gt;&lt;/mechanisms&gt;&lt;/stream:features&gt;
  =&gt; &lt;auth auth:service="oauth2" xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-OAUTH2"&gt;AGNvaW2iaXRzeUBnbWFpbC5jb12AdW5kZWZpbmVk&lt;/auth&gt;
  &lt;= &lt;stream:error&gt;&lt;invalid-xml xmlns="urn:ietf:params:xml:ns:xmpp-streams"/&gt;&lt;/stream:error&gt;
  </code></pre>
  
  <p>It seems node-xmpp tries to use OAUTH2 although I haven't provided any token whatsoever. It should use PLAIN instead.</p>
  
  <p><strong>2. node-xmpp oauth example doesn't work either</strong></p>
  
  <p>Created OAUTH2 token according to node-xmpp pull request #85</p>
  
  <pre><code>$node node_modules/node_xmpp/examples/echo_bot_oauth.js gmailid@gmail.com oauth2token
  No usable SASL mechanism
  </code></pre>
  
  <p><strong>3. Adding <code>preferred:"PLAIN"</code> fixes connection issue</strong></p>
  
  <p>changing options to</p>
  
  <pre><code> var options = {
     jid: "mygoogleid@gmail.com",
     password: "mygooglepassword",
     preferred: "PLAIN"
   };
  </code></pre>
  
  <p>fixes the error and seems to authenticate successfully. This might be evidence for explanation #8 (bug within node-xmpp).</p>