alpacahq / alpaca-ts

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
ISC License
156 stars 41 forks source link

AlpacaStream for type "account" is busted for paper accounts #71

Closed xanoinc closed 2 years ago

xanoinc commented 3 years ago

Description When using type account for AlpacaStream it complains about malformed json.

I did some research and it looks like according to the documentation, the account stream uses binary frames for the paper api while the live stream uses text frames. I'm not sure why, but that seems to be causing an issue.

See note from - https://alpaca.markets/docs/api-documentation/api-v2/streaming/

Expected

Expecting paper support to work just like normal support.

Reproduction Steps we can take to reproduce the bug:

Just create a stream using a paper account and trade_updates channel and it will complain about bad json

Logs If applicable, add logs to help explain the bug.

Additional Add any other context about the problem here.

117 commented 3 years ago

Good catch.

117 commented 3 years ago

I have been swamped with my current project lately but I will get to this by end of week!

xanoinc commented 3 years ago

I think I may have fixed this in stream.ts. So far this appears to work.

          this.connection.onmessage = async (event) => {
              let data = event.data
              if (data instanceof Blob) {
                data = await event.data.text()
              }

              let parsed = JSON.parse(data), messages = this.params.type == 'account' ? [parsed] : parsed;
117 commented 3 years ago

Will try your fix out.

117 commented 3 years ago

Just tested your fix, works great! Thanks for the contribution, version 6.2.2 has this update implemented.

awweather commented 2 years ago

@117 @seanmetrix The Blob type doesn't exist in Nodejs, only the browser. So this breaks any attempt to access the stream.

FallingSnow commented 2 years ago

@117 You need to add the following maybe?

import { Blob } from "node:buffer";
117 commented 2 years ago

@FallingSnow @awweather This should be resolved in 6.2.6.