Suggestions

close search

Connecting

Connecting the Stringee Client to Stringee's server allows authentication to procede and data to flow.

How to do it

The Stringee Client is initialized with a context.

1. Create client

Only one instance of StringeeClient should be instantiated and used at all times.

  constructor(props) {
    super(props);
    ...
    this.client = createRef();
    ...
    }
  ...
  render() {
    return (
      <View style={styles.container}>
        <StringeeClient ref={this.client}/>
      </View>
    );
  }

2. Listners

StringeeClient uses listner pattern to notify your app about specific events. You need to implement listeners for connection and object (Conversation, Message) change events.

  constructor(props) {
    super(props);
    this.clientEventHandlers = {
      onConnect: this.clientDidConnect,
      onDisConnect: this.clientDidDisConnect,
      onFailWithError: this.clientDidFailWithError,
      onRequestAccessToken: this.clientRequestAccessToken,
      onObjectChange: this.onObjectChange
      onUserBeginTyping: this.onUserBeginTyping,
      onUserEndTyping: this.onUserEndTyping,
    };
  }

  render() {
    return (
      <View style={styles.container}>
        <StringeeClient ref={this.client} eventHandlers={this.clientEventHandlers} />
      </View>
    );
  }

3. Connect client

Once you have initialized Stringee Client and registered listeners, connect the SDK:

  async componentDidMount() {
    await this.client.current.connect("your_access_token");
  }

See Authentication to get your access token.

4. Disconnect client

You can disconnect Stringee Client by calling:

this.client.current.disconnect();

5. Local database

Stringee stores the authenticated user's data (Conversations, Messages, Participants...) in a local database. If your app wants to authenticate with another user, you must clear the local database first by calling:

    this.client.current.clearDb((status, code, message) => {
      console.log("status-" + status + " code-" + code + " message-" + message);
    });