node-ib icon indicating copy to clipboard operation
node-ib copied to clipboard

net.connect is not a function when calling ib.connect()

Open dreamrockz opened this issue 7 years ago • 5 comments
trafficstars

Hi,

I am using node_ib with electron and angular 5! When calling ib.connect, i get the exception that net.connect is not a function.

I read the other issues, but i don´t understand, why net.connect doesn´t work in this environment. Is there a workaround to still use node-ib in this scenario?

Best regards, Christian

dreamrockz avatar Jan 23 '18 12:01 dreamrockz

@dreamrockz any update on this one? have the same problem.

mr-pascal avatar Oct 20 '18 15:10 mr-pascal

Hi,

Think it has to do with the webpack configuration. You need to make sure, that you require ib into the correct scope. Otherwise it is not visible. Working for me in these two cases:

  1. create class to handle all ib calling outside of renderer
  2. Inside renderer scope you need to require it from electron remote

Hope that helps!

dreamrockz avatar Oct 20 '18 16:10 dreamrockz

Hi, I don't use webpack or electron. I simply created a new angular project (v7, but also didn't worked wiht v5) installed ib via npm install ib and then used following code in my app.component.ts to test:

import {
  Component
} from '@angular/core';
import * as ibS from 'ib';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'app';


  constructor() {

  }

  public init(): void {
    const ib = new ibS({
      clientId: 10,
      host: '127.0.0.1',
      port: 7497
    });

    ib.on('error', function (err) {
      console.error('error --- %s', err.message);
    }).on('result', function (event, args) {
      console.log('%s --- %s', event, JSON.stringify(args));
    }).once('nextValidId', function (orderId) {
      ib.placeOrder(
        orderId,
        ib.contract.stock('AAPL'),
        ib.order.limit('BUY', 1, 0.01) // safe, unreal value used for demo
      );
      ib.reqOpenOrders();
    }).once('openOrderEnd', function () {
      ib.disconnect();
    });

    ib.connect()
      .reqIds(1);
  }

}

mr-pascal avatar Oct 20 '18 17:10 mr-pascal

Hi,

for me it only works with the good old require like this:

this.ib = new (require("ib"))({ clientId: clientId, host: config.host, port: config.port })

Best of luck...

dreamrockz avatar Oct 20 '18 17:10 dreamrockz

also imported ib with npm, though!

dreamrockz avatar Oct 20 '18 17:10 dreamrockz