Search code examples
react-nativeopentok

Can I access event handlers from ref OTSession


I'm trying acess events coming from OTSession by ref like streamCreated, signal, streamDestroyed. Is that possible like that: How I think it should work


Solution

  • Manik here from TokBox.

    You cannot access the events through a ref because we pass the events to the native layer and set the listeners when mounting the component, however, you can listen to the events like so:

    import React, { Component } from 'react';
    import { View } from 'react-native';
    import { OTSession, OTPublisher, OTSubscriber } from 'opentok-react-native';
    
    export default class App extends Component {
      constructor(props) {
        super(props);
        this.apiKey = '';
        this.sessionId = '';
        this.token = '';
        this.sessionEventHandlers = {
          streamCreated: event => {
            console.log("stream created", event);
          },
          streamDestroyed: event => {
            console.log("stream destroyed", event);
          },
        };
      }
    
      render() {
        return (
          <View style={{ flex: 1, flexDirection: 'row' }}>
            <OTSession apiKey={this.apiKey} sessionId={this.sessionId} token={this.token} eventHandlers={this.sessionEventHandlers}>
              <OTPublisher style={{ width: 100, height: 100 }} />
              <OTSubscriber style={{ width: 100, height: 100 }} />
            </OTSession>
          </View>
        );
      }
    }