# TCPClient and NodeJS server

**URL:** <https://community.particle.io/t/tcpclient-and-nodejs-server/26657>\
**Category:** Getting Started\
**Created:** [October 21, 2016, 10:40am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657 "2016-10-21T10:40:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jelmertje](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jelmertje](https://community.particle.io/u/Jelmertje)\
**Post date:** [October 21, 2016, 10:40am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657/1 "2016-10-21T10:40:53Z")

</div>

Hi there,

I’m trying to get a Photon to communicate with a NodeJS Server, but I don’t get it to work.

I have a NodeJS server listening on port 3700 and I have this code flashed to the core:

```
TCPClient client;
byte server[] = { my, ip, add, res };

void setup() {
    pinMode(D7, OUTPUT);
}

void loop() {
    if(client.connect(server, 3700)){
        digitalWrite(D7, HIGH);
    }
    delay(1000);
}

```

But D7 does not turn on. Does this mean I’m not connecting at all? What could be going wrong?

SOLVED: My photon could not connect because I needed to forward my port to my device in my router. For a full explanation scroll down and read rickkas7 post.

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [October 21, 2016, 10:47am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657/2 "2016-10-21T10:47:58Z")

</div>

I’m not sure what the issue might be, but some of these topics might offer some help:

> **[Sending large data off Photon using TCPClient](http://community.particle.io/t/sending-large-data-off-photon-using-tcpclient/20791)**
>
> I was wondering exactly how fast you can get data off a Photon using a direct TCP connection (TCPClient), and how reliable is it? Well, after tweaking the code and some parameters, in my somewhat arbitrary test of opening a TCP connection and...

  

> **[Local server in node.js example](http://community.particle.io/t/local-server-in-node-js-example/24233)**
>
> The event publish and subscribe feature of the Particle Cloud is easy-to-use and very effective in most cases. But sometimes you need to transmit more data than is allowed for publishing events. Or maybe you need to do some specialized processing. A...

  

> **[Photon/node.js live streaming graph web page example](http://community.particle.io/t/photon-node-js-live-streaming-graph-web-page-example/22286)**
>
> I got a request this morning for a tutorial/example of streaming data from a Photon to a local node.js server, and then graphing it on a web page. I thought that sounded interesting, so here it is. It’s currently sending a sample every 20...

  

> **[Sending large amounts of data using TCPClient on Photon (revisited)](http://community.particle.io/t/sending-large-amounts-of-data-using-tcpclient-on-photon-revisited/21861)**
>
> The Photon can stream out a whole lot of data using TCPClient, quite reliably, and pretty quickly, at least under ideal conditions. I had a done a test sending out 1 Mbyte of data, then closing and reopening the connection earlier, but the question...

  

> **[TCPServer example using SYSTEM\_MODE(MANUAL)](http://community.particle.io/t/tcpserver-example-using-system-mode-manual/20995)**
>
> There was recently a question about adapting the TCPServer telnet example code for use in MANUAL mode so your own code could run even when the network goes away. I wrote a little sample program that does this, and also instruments the amount of time...

---

<div class="post-metadata">

**Author:** ![Jelmertje](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jelmertje](https://community.particle.io/u/Jelmertje)\
**Post date:** [October 21, 2016, 11:01am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657/3 "2016-10-21T11:01:05Z")

</div>

Thanks, I guess I’ll read those first.

---

<div class="post-metadata">

**Author:** ![Jelmertje](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jelmertje](https://community.particle.io/u/Jelmertje)\
**Post date:** [October 22, 2016, 9:50am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657/4 "2016-10-22T09:50:15Z")

</div>

Hi again, so I read the examples, and I tried them out too, but they don’t do anything for me. It seems that the photon simply does not connect to my server.

Could this be an IP issue? The applications work fine on localhost in my browser, so I assume the server is running, but all connect attempts fail.  
The IP I pass to my firmware is the one I get from [whatsmyip.org](http://whatsmyip.org) but other computers in my home all share that address so I can imagine that this address is not sufficient.  
I just don’t know how I could solve this issue.

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [October 22, 2016, 9:57am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657/5 "2016-10-22T09:57:22Z")

</div>

I can imagine you’d need the IP address from the actual device rather than from your ISP. There are several apps available with which you can scan your local network, but a look inside your router should offer the same info.

---

<div class="post-metadata">

**Author:** ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)\
**Post date:** [October 22, 2016, 10:46am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657/6 "2016-10-22T10:46:05Z")

</div>

A typical home network looks something like this:

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/1/1f318ba512f99798dc3ae2cfd0599c805790fffa.jpg)

Everything on your home network is assigned an IP address on your local network, typically of the form 192.168.x.y or 10.x.y.z by your router.

In this example, the router itself has an internal IP address of 192.168.1.1, the Photon 192.168.1.130, and so on.

The router also has a connection to the rest of the Internet. All of the home devices share one public IP address, in this example it’s 56.78.99.100.

When you’re doing local connection using TCP or UDP, you always use the internal IP addresses. Say you’re running a node server on the computer 192.168.1.10. If the Photon wanted to make a connection, it would use TCPClient to make a connection to 192.168.1.10.

If Remote Laptop, elsewhere on the Internet, wanted to access any of the internal servers, whether it be the node server on 192.168.1.10 or the Photon directly, you’d need to reconfigure your router _port forwarding_ to connection into the external address 56.78.99.100 are forwarded to an internal server. You should do this with care, because it can expose internal devices to attacks from the Internet.

---

<div class="post-metadata">

**Author:** ![Jelmertje](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jelmertje](https://community.particle.io/u/Jelmertje)\
**Post date:** [October 22, 2016, 11:03am UTC](https://community.particle.io/t/tcpclient-and-nodejs-server/26657/7 "2016-10-22T11:03:38Z")

</div>

Alright, thank you, I looked that up after Moors7 posted about ISP and I found this:

> <https://stackoverflow.com/questions/10598926/access-localhost-from-another-computer-not-on-network>

That also mentioned port forwarding which I managed to do on my router. I tested this out using some other node.js chat example to connect to my localhost:port server on one computer from another.  
This worked fine, so next I’ll be testing it out with my Photon.

Thanks for the explanation!

EDIT: worked fine on my photon as well!
