opentok / opentok-react

React components for OpenTok.js
https://www.npmjs.com/package/opentok-react
MIT License
107 stars 105 forks source link

OTSubcribe might crash when creating a subscriber if a session isn't connected #147

Open HaleXie opened 4 years ago

HaleXie commented 4 years ago

https://github.com/opentok/opentok-react/blob/02ed7bcfe1db3c456c51f4841ea2bfc4c3d6c335/src/OTSubscriber.js#L97

I see this error when a session is disconnected during a reconnecting

OTSubscriber.js:128 Uncaught TypeError: Cannot read property 'on' of undefined
    at OTSubscriber.createSubscriber (OTSubscriber.js:128)
    at OTSubscriber.js:145

OTSubscriber:

  createSubscriber() {
...

    const subscriber = this.state.session.subscribe(
          ...
        );

    if (
            this.props.eventHandlers &&
            typeof this.props.eventHandlers === 'object'
        ) {
      subscriber.on(this.props.eventHandlers); // This line throws the exception
    }

Based the information on this page, this.state.session.subscribe() might return an undefined. I think we should check if the subscriber instance exists before setting up the event handlers.

https://tokbox.com/developer/sdks/js/reference/Session.html#subscribe

yuri1992 commented 3 years ago

We are encountering the sample problem in production.

yuri1992 commented 3 years ago

https://github.com/opentok/opentok-react/pull/176

enricop89 commented 3 years ago

@yuri1992 @HaleXie Are you able to reproduce it with the latest version of JS SDK 2.19.0?

I tried the following steps:

  1. connect and subscribe to a stream
  2. disable Wifi e trigger the reconnecting event
  3. Enable WiFi after 15 seconds and reconnect to the session

I was not able to get the error. Can you share your steps to reproduce, please?

yuri1992 commented 3 years ago

Hey @enricop89, Try to enable the wifi after 60 seconds.

This how we reproduce the error:

  1. Subscribe to Stream
  2. Disable the wifi
  3. Wait 60 seconds, and enable the wifi.
enricop89 commented 3 years ago

@yuri1992 using @opentok/client 2.19.1 I am not seeing this error. After 60 seconds the session goes to the disconnected state, so you won't be able to subscribe to a stream (therefore to trigger the error)