GithubHelp home page GithubHelp logo

Document getSubscriber about opentok-react HOT 13 CLOSED

opentok avatar opentok commented on July 29, 2024
Document getSubscriber

from opentok-react.

Comments (13)

aiham avatar aiham commented on July 29, 2024 1
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.otPublisher = React.createRef();
  }
  getPublisherStats(){
    this.otPublisher.getPublisher().getStats((err, stats) => console.log(err, stats));
  }
  render() {
    return <OTPublisher ref={this.otPublisher} />;
  }
}

from opentok-react.

enricop89 avatar enricop89 commented on July 29, 2024 1

I think you can also do something like this:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.otPublisher = React.createRef();
  }
  getPublisherStats(){
    this.otPublisher.getPublisher().getStats((err, stats) => console.log(err, stats));
  }
  render() {
    return <OTPublisher ref={(instance) => { this.otPublisher = instance }} />;
  }
}

Using the instance inside the ref function (ref={(instance) => { this.otPublisher = instance }}), you access the OTPublisherdirectly.

from opentok-react.

aiham avatar aiham commented on July 29, 2024

From https://github.com/opentok/opentok-react#otpublisher-component

To facilitate this the Publisher instance is exposed via the getPublisher() component method. You should always call this method to get the latest Publisher instance instead of keeping a reference to it as it's possible for the Publisher instance to change, leaving you with a stale reference.

and

There are plans to support more Publisher properties but for now you will have to call getPublisher() to retrieve the Publisher instance and make the necessary changes yourself.

It's not ideal but this was low priority when first implemented. Feel free to submit a PR if you'd like this improved.

from opentok-react.

wyqydsyq avatar wyqydsyq commented on July 29, 2024

Yes that tells you to use the getPublisher method of OTPublisher component instance in order to change the OT.Publisher instance, what I am saying is there is no documentation or examples on how getPublisher is actually supposed to be used.

How am I supposed to call getPublisher from the OTPublisher instance? Neither of the below works:

  render(){
    const p = <OTPublisher {....props} />;
    console.log(p.getPublisher());
  }

-> getPublisher is not a function

  render(){
    const p = new OTPublisher(...props);
    console.log(p.getPublisher());
  }

-> getPublisher is not a function

I am only able to find the method being used twice in the entire project, both times it is being used in the context of enzyme tests where enzyme makes the instance easily accessible with wrapper.instance(): https://github.com/opentok/opentok-react/search?q=getPublisher&unscoped_q=getPublisher

I'm happy to submit a PR to add documentation on how it is intended to be used, but first I need to understand that myself.

from opentok-react.

aiham avatar aiham commented on July 29, 2024

Good point. I use it in this SO answer: https://stackoverflow.com/a/49889581/545726

from opentok-react.

wyqydsyq avatar wyqydsyq commented on July 29, 2024

The above does not work. It results in this.otPublisher having a value of

 {current: OTPublisher}

Which does not have a .getPublisher() method.

from opentok-react.

aiham avatar aiham commented on July 29, 2024

So then use current: this.otPublisher.current.getPublisher()

You can figure this out on your own by looking up the documentation for React.createRef

from opentok-react.

wyqydsyq avatar wyqydsyq commented on July 29, 2024

this.otPublisher.current appears to be an actual instance of OTPublisher and also lacks a getPublisher method, I have no idea whether the current property will actually be dynamically returning a live reference of the current OTPublisher that is being managed under the hood or just the initial one I already have a reference of

image

from opentok-react.

aiham avatar aiham commented on July 29, 2024

@wyqydsyq getPublisher should be on the prototype. Did you try calling it? It comes from here: https://github.com/opentok/opentok-react/blob/master/src/OTPublisher.js#L61

from opentok-react.

ashifa454 avatar ashifa454 commented on July 29, 2024

@wyqydsyq did you found the solution, because i am having the same issue.

from opentok-react.

marifehe avatar marifehe commented on July 29, 2024

I was facing a similar issue, but with the subscriber. In order to have access to the object that getSubscriber returns I had to wait for componentDidMount but also wait for the next animation frame.

So having this:

componentDidMount() {
    requestAnimationFrame(() => {
      console.log('>>>>> 2: ', this.otSubscriber.current.getSubscriber());
    });
    
    console.log('>>>>> 1: ', this.otSubscriber.current.getSubscriber())
  }

1 prints null, but 2 prints the subscriber object. This makes sense, according to React lifecycle and how componentDidMount works. So make sure you're not accessing your refs object too early.

from opentok-react.

wyqydsyq avatar wyqydsyq commented on July 29, 2024

@ashifa454 the suggestion made by @aiham ended up working for me, just had to use .current as mentioned above. I'll try to get around to submitting a PR to add this to the docs

from opentok-react.

msach22 avatar msach22 commented on July 29, 2024

We've gone ahead and documented this. Closing the issue!

from opentok-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.