<p>I'm actually not sure how, but now everything works.</p>

<p>I'm writing here a few things to be kept in mind to make it work:</p>

<p>1) the port number is that of the <code>bitcoind</code> server (8333 for mainnet, 18333 for testnet)</p>

<p>2) in case of testnet, the network must be passed as an option to the Peer constructor</p>

<p>3) node version must be 4.x (use <code>nvm use 4</code>), otherwise the code will raise an exception</p>

CC BY-SA 3.0
2016-11-15T11:10:00.067Z
2016-11-15T11:10:00.067Z
FedFranz
<p>I'm not sure what exactly you are trying to do with:</p>
<pre class="lang-js prettyprint-override"><code>peer.on('connected', function(){ 
 console.log('connected'); 
});
</code></pre>
<p>You can just do on <code>ready</code> instead.</p>
<pre class="lang-js prettyprint-override"><code>peer.on('ready', function(){
  console.log('connected'); 
});
</code></pre>
<p><strong>Example connecting to a peer and receiving notifications</strong></p>
<pre class="lang-js prettyprint-override"><code>var bitcore = require('bitcore-lib');
var Peer = require('bitcore-p2p').Peer;
    
var peer = new Peer({host: &quot;101.201.31.79&quot;});
//IP of one of the btc.com's miners
    
peer.on('ready', function(){
    console.log(&quot;Connected &quot;+peer.version, 
    peer.subversion, peer.bestHeight);
});
peer.on('disconnect', function(){
    console.log(&quot;disconnected&quot;);
});
//loop through peer notifications
peer.on('inv', function(message){
    var nodeMsg = message.inventory;
    var nodeMsgLen = nodeMsg.length;
    for (var i = 0; i &lt; nodeMsgLen; i++){
        console.log(nodeMsg[i].hash);
    };
});
//connect
peer.connect();
</code></pre>

CC BY-SA 3.0
2016-11-15T10:14:55.203Z
I had already tried with every event. Maybe the 'ready' event is more approriate (I will update my question). The problem is that it disconnect as soon as it tried to connect. Furthermore, the same code works for livenet. I did a little debug and found out that, in the testnet case, it sends a wrong (I guess) message to the peer, where the 'network' object is set to livenet instead of testnet. I am updating the issue I opened on github. Hope the developers will help
FedFranz
CC BY-SA 4.0
2016-11-15T05:21:33.003Z
2024-03-04T10:16:26.600Z
2024-03-04T10:16:26.600Z
m1xolyd1an
<p>I'm trying to run a simple script to connect to a Bitcoin peer, like this one:</p>
<pre class="lang-js prettyprint-override"><code>  var bitcore = require('bitcore-lib')
  var p2p = require('bitcore-p2p')
  var Peer = p2p.Peer;
  
  var peer = new Peer({host: 'localhost', port: 18333, network: Networks.testnet})
  peer.on('connect', function () { console.log('connect'); })
  peer.on('ready', function(){  console.log('ready'); });
  peer.on('disconnect', function() {  console.log('connection closed'); });
  peer.connect()
</code></pre>
<p>However, the connection seems to never be established.
I only get the 'connect' and the 'connection closed' output, but not 'connected'.
The port I set is the one in the Bitcore config file.</p>
<p>Can someone please help me figure out what is the problem?</p>

CC BY-SA 3.0
2016-11-14T17:14:43.740Z
It sounds like you're connecting to yourself.
Pieter Wuille
CC BY-SA 3.0
2016-11-14T17:34:39.903Z
I am connecting to a local node. But this code is separated.
FedFranz
<peers><bitcore>
Bitcore-p2p: Peer.connect does not work
FedFranz
# Bitcore-p2p: Peer.connect does not work

(Asked by: username_0 on 2016-11-14)

I'm trying to run a simple script to connect to a Bitcoin peer, like this one:

```js
  var bitcore = require('bitcore-lib')
  var p2p = require('bitcore-p2p')
  var Peer = p2p.Peer;
  
  var peer = new Peer({host: 'localhost', port: 18333, network: Networks.testnet})
  peer.on('connect', function () { console.log('connect'); })
  peer.on('ready', function(){  console.log('ready'); });
  peer.on('disconnect', function() {  console.log('connection closed'); });
  peer.connect()

```

However, the connection seems to never be established.
I only get the 'connect' and the 'connection closed' output, but not 'connected'.
The port I set is the one in the Bitcore config file.

Can someone please help me figure out what is the problem?

**Tags:** peers, bitcore

> **Comments:**
> **username_1**: It sounds like you're connecting to yourself.  
> **username_0**: I am connecting to a local node. But this code is separated.  

---

## 2 answers

### Answer by username_0 on 2016-11-15. Score: 0 | Accepted answer

I'm actually not sure how, but now everything works.

I'm writing here a few things to be kept in mind to make it work:

1) the port number is that of the `bitcoind` server (8333 for mainnet, 18333 for testnet)

2) in case of testnet, the network must be passed as an option to the Peer constructor

3) node version must be 4.x (use `nvm use 4`), otherwise the code will raise an exception


---

### Answer by username_2 on 2016-11-15. Score: 0

I'm not sure what exactly you are trying to do with:

```js
peer.on('connected', function(){ 
 console.log('connected'); 
});

```

You can just do on `ready` instead.

```js
peer.on('ready', function(){
  console.log('connected'); 
});

```

**Example connecting to a peer and receiving notifications**

```js
var bitcore = require('bitcore-lib');
var Peer = require('bitcore-p2p').Peer;
    
var peer = new Peer({host: "101.201.31.79"});
//IP of one of the btc.com's miners
    
peer.on('ready', function(){
    console.log("Connected "+peer.version, 
    peer.subversion, peer.bestHeight);
});
peer.on('disconnect', function(){
    console.log("disconnected");
});
//loop through peer notifications
peer.on('inv', function(message){
    var nodeMsg = message.inventory;
    var nodeMsgLen = nodeMsg.length;
    for (var i = 0; i < nodeMsgLen; i++){
        console.log(nodeMsg[i].hash);
    };
});
//connect
peer.connect();

```

> **Comments:**
> **username_0**: I had already tried with every event. Maybe the 'ready' event is more approriate (I will update my question). The problem is that it disconnect as soon as it tried to connect. Furthermore, the same code works for livenet. I did a little debug and found out that, in the testnet case, it sends a wrong (I guess) message to the peer, where the 'network' object is set to livenet instead of testnet. I am updating the issue I opened on github. Hope the developers will help  

---

