Skip to content

Commit

Permalink
Pass full json message in account streamer; update README with instru…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
dmoss18 committed Jul 3, 2023
1 parent 3b2ac87 commit 4c4a273
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 48 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ const optionChain = await tastytradeClient.instrumentsService.getOptionChain('AA
quoteStreamer.subscribe(optionChain[0]['streamer-symbol'], handleMarketDataReceived)
```

### Account Streamer
```js
const TastytradeApi = require("@tastytrade/api")
const TastytradeClient = TastytradeApi.default
const { AccountStreamer, QuoteStreamer } = TastytradeApi
const _ = require('lodash')

function handleStreamerMessage(json) {
console.log('streamer message received: ', json)
}

function handleStreamerStateChange(streamerState) {
console.log('streamer state changed: ', streamerState)
}

const tastytradeClient = new TastytradeClient(baseUrl, accountStreamerUrl)
const accountStreamer = tastytradeClient.accountStreamer
const loginResponse = await tastytradeClient.sessionService.login(usernameOrEmail, password)
const accounts = await tastytradeClient.accountsAndCustomersService.getCustomerAccounts()
const accountNumbers = _.map(accounts, account => _.get(account, 'account.account-number'))
await accountStreamer.start()
await accountStreamer.subscribeToAccounts(accountNumbers)
accountStreamer.addMessageObserver(handleStreamerMessage)
accountStreamer.addStreamerStateObserver(handleStreamerStateChange)
```

You should then be able to place a trade and see live status updates for the order come through via `handleStreamerMessage`.

## Running in Node
The `cometd` package has an explicit reference to `window`, so there's not a perfect way to run this code in a NodeJs. You could fake the `window` object to get it running. You'll have to `npm install ws` and do this:

```js
const WebSocket = require('ws')

global.WebSocket = WebSocket
global.window = { WebSocket, setTimeout, clearTimeout }
```

## Building Locally
`npm run build`
Outputs everything to `dist/`
Expand Down
48 changes: 4 additions & 44 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"lint": "next lint"
},
"dependencies": {
"@dxfeed/api": "^1.1.0",
"@types/lodash": "^4.14.182",
"axios": "^1.3.4",
"dayjs": "^1.11.1",
Expand Down
4 changes: 2 additions & 2 deletions lib/account-streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Disposer = () => void

export type StreamerStateObserver = (streamerState: STREAMER_STATE) => void

export type StreamerMessageObserver = (messageType: string, action: string, status: string) => void
export type StreamerMessageObserver = (json: object) => void

const REQUEST_ID = 'request-id'

Expand Down Expand Up @@ -368,7 +368,7 @@ export class AccountStreamer {
this.logger.info(json)

const action = json.action as string
this.streamerMessageObservers.forEach(observer => observer(json.type as string, action, json.status as string))
this.streamerMessageObservers.forEach(observer => observer(json))
if (action) {
if (action === MessageAction.HEARTBEAT) {
// schedule next heartbeat
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/service/session-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('logout', () => {
const client = new TastytradeHttpClient('fakeurl')
client.session.authToken = 'faketoken'
const sessionService = new SessionService(client)
const response = await sessionService.logout()
await sessionService.logout()
expect(client.session.authToken).toBeNull()
})
})

0 comments on commit 4c4a273

Please sign in to comment.