title: Connecting to zookeeper with Node.js
body: <p>I'm trying to connect to a Zookeeper server using the <a href="https://github.com/alexguan/node-zookeeper-client" rel="nofollow">node-zookeeper-client</a> library. </p>
  
  <p>The code appears to work properly, except for one case which I cannot detect: when the server is not available and I start the client. In this case the client just hangs, without timeout (no events seem to be emited). How can I detect this situation? </p>
  
  <pre><code>var zookeeper = require("node-zookeeper-client");
  
  console.log("connecting to zookeeper");
  
  zk = new zookeeper.createClient("localhost:2181", {
          sessionTimeout: 10,
          spinDelay : 10,
          retries : 0
  });
  
  zk.on("connected", function () {
      console.log("connected");
  });
  
  zk.on("connectedReadOnly", function () {
      console.log("connected read-only");
  });
  
  zk.on("expired", function () {
      console.log("zk session expired");
  });
  
  zk.on("disconnected", function (){
      console.log("disconnected from zk");
  });
  
  zk.on('state', function (state) {
      console.log("state : %s", state);
  });
  
  zk.connect();
  </code></pre>
  
  <p>I'm aware that I could use something like this to detect the situation, but it feels like a hack. </p>
  
  <pre><code>setTimeout(function () {
      /* am I still in the DISCONNECTED state? */
      console.log("client state: %s", zk.getState());
  }, 5000);
  </code></pre>