Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly disconnecting from IB Gateway #221

Open
kfiroo opened this issue Aug 8, 2024 · 1 comment
Open

Properly disconnecting from IB Gateway #221

kfiroo opened this issue Aug 8, 2024 · 1 comment

Comments

@kfiroo
Copy link

kfiroo commented Aug 8, 2024

Hey!
First of all, thanks for all the effort! it is much appreciated ❤️

I am working with the new IBApiNext and I am wondering on what is the best way to disconnect a client.

I am connecting using a random clientId and doing my best to call disconnect when my work is done but in my IB Gateway app I see that the API Client is still connected.

This didn't happen to me when I used the python module ib_insync and I am wondering if there is something that I am doing wrong or is it the expected behavior?

Sample code:

const clientId = getRandonId()

const apiNext = new IBApiNext({
  port: configuration.IB_PORT,
})

// Subject for connected - I guess this is not really needed as you do it internally right?
const connected$ = apiNext.connectionState.pipe(
  filter((state) => state === ConnectionState.Connected),
  first(),
)

try {
  apiNext.connect(clientId)

  await firstValueFrom(connected$)

  await doSomethingWithIBApi(apiNext)
} catch (error) {
  logger.error(error)
  throw error
} finally {
  // not this this actually disconnects me :(
  apiNext.disconnect()
}

Thanks in advance 🙏🏼

@rylorin
Copy link
Member

rylorin commented Nov 10, 2024

Hello,

I tested your code fragment with:

import { filter, first, firstValueFrom } from "rxjs";
import { ConnectionState, IBApiNext } from "..";

const getRandonId = () => 12;

const sleep = (secs: number) => new Promise((r) => setTimeout(r, secs * 1000));

const doSomethingWithIBApi = async (..._args) => {
  await sleep(10);
};

async function main() {
  const clientId = getRandonId();

  const apiNext = new IBApiNext();

  // Subject for connected - I guess this is not really needed as you do it internally right?
  const connected$ = apiNext.connectionState.pipe(
    filter((state) => state === ConnectionState.Connected),
    first(),
  );

  try {
    apiNext.connect(clientId);

    await firstValueFrom(connected$);
    console.log("connected");

    await doSomethingWithIBApi(apiNext);
  } catch (error) {
    console.error(error);
    throw error;
  } finally {
    // not this this actually disconnects me :(
    apiNext.disconnect();
    console.log("disconnected");
    await sleep(10);
  }
}

main();

I connect, then disconnect after 10 secs, and I've been able to seen that the client properly disconnected from the API.
Your issue is maybe related to some other problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants