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

Support async operations in unpackMessage() #34

Closed
cameronelliott opened this issue Dec 11, 2020 · 4 comments
Closed

Support async operations in unpackMessage() #34

cameronelliott opened this issue Dec 11, 2020 · 4 comments

Comments

@cameronelliott
Copy link

I've been experimenting with this library in the browser,
but run into one issue I am not sure how to resolve.

const wsp = new WebSocketAsPromised(wsUrl, {
  packMessage: data => JSON.stringify(data),
 unpackMessage: data => JSON.parse(data)
});

For the unpackMessage function, in the browser, I receive a Blob, but I don't think I can get the Blob data before returning from the function. When I tried JSON.parse it just returned undefined.
Maybe data is not a blob in Node, but it seems to be in Chrome.
Let me know if an example would help.

@vitalets
Copy link
Owner

but I don't think I can get the Blob data before returning from the function

Could you demonstrate this?

@cameronelliott
Copy link
Author

@vitalets Vitaly, I really love this library, and your work in general, but I am working on a different project right now, so I think it might be a long time before I could get to this.
The main issue as best as I can explain it: (if I am not mistaken)

I think you can read the contents of the Blob inside the function when using Node.js (not sure, I am browser focused)
But in the browser environment, I think, I believe you cannot read the contents of the Blob without awaiting a promise,
which cannot be done inside unpackMessage
Blob.prototype.arrayBuffer() returns a promise.
Blob.prototype.stream() gives a ReadableStream which eventually requires a promise to read the data.
Blob.prototype.text() returns a promise.

So, my understanding, as I mid-level JS developer, is that I need to await a promise to get the data from the Blob, in order to process/unpack it, and return it from unpackMessage

I hope that helps.
I will close this since I cannot write an example at this time.
Thank you for writing this impressive library.

@vitalets
Copy link
Owner

@cameronelliott I've got the point.
Yes, it seems the library should go more clever way: if unpackMessage returns promise - wait for it.
I'll reopen the issue and update the title.

Thanks for your explanation anyway!

@vitalets vitalets reopened this Jan 26, 2021
@vitalets vitalets changed the title Possible issue with unpack in browser Support async operations in unpackMessage() Jan 26, 2021
@vitalets
Copy link
Owner

Released as v2.0.1.
Feel free to report any problems!

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