ib
ib copied to clipboard
Properly disconnecting from IB Gateway
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 🙏🏼