title: transferring data through tcp sockets on a remote server using nodejs
body: <p>I’m building a Nodejs TCP module, a server/client application to send data through tcp sockets. I have this module working on my localhost with no issues but when I send data from my localhost to a remote server It doesn’t work. I’m getting weird results because I can see the header of my data in my nginx access logs, so I know the data is being transferred but my server side application isn’t saving the data as expected. I’m clearly missing something. I’ve tried playing around with the ip-addresses and a few nginx-conf files I found online but have had no success. I’m not sure if I need to open a port or manipulate a firewall but any help is greatly appreciated Here’s the node app and my nginx.conf files.</p>
  
  <h1>TCP Client App</h1>
  
  <pre><code>/* Dependencies */
  var server_external_ip = '77.77.77.277'
  var fs = require('fs');
  var hl7 = require('simple-hl7');
  /* Build TCP Server */
  var server = hl7.Server;
  var tcpClient = server.createTcpClient();
  /* Connection */
  tcpClient.connect('127.0.0.1', 6969);
  /* Get XML  */
  var msg = fs.readFileSync('./data/example.xml').toString();
  /* Send Message */
  setTimeout(function() {
      tcpClient.send(msg, function(ack) {
        console.log("ACK: ",ack.toString());
        console.log("\nsuccessful transfer");
        tcpClient.close();
      });
  }, 500);
  </code></pre>
  
  <h1>the server can be found here, <a href="https://www.snip2code.com/Snippet/148120/A-simple-node-js-HL7-MLP-server--Saves-m" rel="nofollow">click here</a> but heres the config code..</h1>
  
  <pre><code>  /*
        Config class
        vars:
        baseFolder: Where to save the messages
        port      : What port to listen on
        ip        : what ip to listen on
    */
    var Config = new function() {
        this.baseFolder = "data";
        this.port = 6969;
        this.ip = '127.0.0.1';
    }
  </code></pre>
  
  <h1>heres the nginx.conf</h1>
  
  <pre><code>user  nginx;
  worker_processes  1;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  
  events {
      worker_connections  1024;
  }
  
  
  http {
      include       /etc/nginx/mime.types;
      default_type  application/octet-stream;
  
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
  
      access_log  /var/log/nginx/access.log  main;
  
      sendfile        on;
      #tcp_nopush     on;
  
      keepalive_timeout  65;
  
      #gzip  on;
  
      #include /etc/nginx/conf.d/*.conf;
      include /etc/nginx/sites-enabled/*;
  
  }
  </code></pre>
  
  <h1>in my sites-enabled directory</h1>
  
  <pre><code>upstream backend_nodejs {
    # hash $remote_addr consistent;
    server 192.168.1.41:9696;
    keepalive 512;
  }
  
  server {
    listen 80;
  
    client_max_body_size 16M;
    keepalive_timeout 10;
  
    location / {
      proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
      proxy_set_header Connection "";
      proxy_http_version 1.1;
      proxy_pass http://backend_nodejs;
    }
  
  }
  </code></pre>
  
  <h1>and finally heres an example of my nginx access log</h1>
  
  <pre><code>xx.xx.xx.123 - - [11/Mar/2016:16:18:27 -0800] "Connection Established" 400 172 "-" "-" "-"
  </code></pre>