pilwon / node-ib

Interactive Brokers TWS API client library for Node.js
382 stars 134 forks source link

It only connect with localhost #154

Open frodoe7 opened 4 years ago

frodoe7 commented 4 years ago

here's my codes

in localhost and it works well

const ib = require('ib');

    let client = new ib({ clientId : 1 , host : 'localhost' , port : 7497 });
    client.connect();
    client.on('connected' , () => {
        console.log("success");
    }).on("error" , () => {
        console.log("error");
    });

here , it's hosted in DigitalOcean server

const ib = require('ib');

    let client = new ib({ clientId : 1 , host : 'my public ip' , port : 7497 });
    client.connect();
    client.on('connected' , () => {
        console.log("success");
    }).on("error" , () => {
        console.log("error");
    });

it does not work in this case , my TWS are configured to receive from external IPs, not only the localhost (I also added my DigitalOcean server public ip to my TWS trusted IPs)

DigitalOcean blocks all the ports by default (except 80) , I also opened 7497 but still the same issue

frodoe7 commented 4 years ago

Also , check this question on SOF : https://stackoverflow.com/questions/60791255/direct-communication-from-a-remote-server-to-application-in-my-device

tredondo commented 4 years ago

This isn't really a question about this library; it's about connectivity. The library doesn't do anything different depending on where it's run. I've been running it for years both locally and on VPSes.

I also added my DigitalOcean server public ip to my TWS trusted IPs

If TWS runs on the DO server, you need to add your client's public IP to the TWS trusted IPs list, not your DO server's IP.

Anyway, that's an insecure setup. I wouldn't trust TWS's SSL. They make it optional to begin with, which is patently stupid.

A secure setup is to only allow TWS to accept connections from localhost, then setup port forwarding on your PC using ssh -nNT -L 7497:localhost:7497 <yourServerIp>. That way you don't need to open port 7497 on the server; only the SSH port (which you should change to a random number above 1024 for additional security).

Also, you probably want to run Gateway v972 (not later because IB fucked it up and it disconnects every day) on the server, and use IBC Alpha to keep it running indefinitely.