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

rxjs/webSocket should support deserializer return Promise #4270

Open
iamcco opened this issue Oct 18, 2018 · 6 comments
Open

rxjs/webSocket should support deserializer return Promise #4270

iamcco opened this issue Oct 18, 2018 · 6 comments
Assignees

Comments

@iamcco
Copy link

iamcco commented Oct 18, 2018

Feature Request

rxjs/webSock should support deserializer return Promise

Is your feature request related to a problem? Please describe.

import { webSocket } from 'rxjs/webSocket'

const api = (options) => {
  const subject = webSocket(options);
  return {
    subscribe: (params: any) => {
      const observableA = subject.multiplex(
        () => params,
        () => ({ close: true }),
        (message: any) =>  {
          return message.requestId === params.requestId
        }
      );

      return observableA
    }
  }
}

const ws = api({
  url: 'ws://127.0.0.1:50055/ws/v2',
  protocol: 'json',
  binaryType: 'blob',
  deserializer: ({ data }) => {
    return new Promise((res, rej) => {
      const reader = new FileReader()
      reader.readAsText(data)
      reader.onload = () => {
        res(JSON.parse(reader.result))
      }
    })
  }
})

ws.subscribe({requestId: '1', data:{}})
  .subscribe((res) => {
    console.log('res', res);
  })

the deserializer have to return Promise because i have to resolve Blob, but socket.onmessage doesn't support deserializer function to return Promise

@benlesh
Copy link
Member

benlesh commented Sep 1, 2020

So the goal of this is to do some additional processing to the raw stream data as it arrives?

I think I see the issue, if you have some format that requires you do something asynchronous to deserialize it, you need to do it here, especially if you're multiplexing the socket. 🤔

@benlesh benlesh self-assigned this Sep 1, 2020
@benlesh
Copy link
Member

benlesh commented Sep 1, 2020

(Sorry it took me so long to find this one, BTW) :(

@iamcco
Copy link
Author

iamcco commented Sep 2, 2020

So the goal of this is to do some additional processing to the raw stream data as it arrives?

I think I see the issue, if you have some format that requires you do something asynchronous to deserialize it, you need to do it here, especially if you're multiplexing the socket. 🤔

Yes 💯

@iamcco iamcco changed the title rxjs/webSock should support deserializer return Promise rxjs/webSocket should support deserializer return Promise Sep 2, 2020
@h4de5
Copy link

h4de5 commented Mar 11, 2021

just to clarify: would this allow us to offload json parsing onto a web worker and away from the main UI thread?

@RichardWright
Copy link

RichardWright commented Jan 31, 2022

@h4de5 you could but that would be pointless. objects are serialised when going between threads even in shared memory buffers.

The point of allowing this function to be a promise is that it allows control to the event loop to be given back. Slower but less blocking.

@benlesh Is this going to be taken on anytime soon? I have very few options when it comes to the synchronous parsing of javascript. reason for async? Breaking up json parsing.

@bdbai
Copy link

bdbai commented Aug 31, 2024

+1
The use case here is I want to encrypt and decrypt the messages using Web Crypto APIs which are unfortunately returning a Promise.

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

5 participants