Suggestions

close search

Connecting

Setting up authentication

In order to connect to Stringee Server, you will need access to the authentication credential: access_token. In production, the access_token should be generated by your server as described in Client authentication. Sample code is available in the access token examples.

But to speed things up we will just hard code the value for now:

  1. In your Activity, declare a variable to store the access_token.
    private String token;
  2. Adjust the code by hard coding the value for the token:

To do this, go to Dashboard -> Tools -> Generate Access token and generate an access_token.

Connecting

Next, we will connect to Stringee Server. You must do this before you can send or receive a message

  1. Add a StringeeClient property to your Activity

    private StringeeClient client;
    private final StringeeConnectionListener connectionListener = new StringeeConnectionListener() {
       @Override
       public void onConnectionConnected(StringeeClient stringeeClient, boolean isReconnecting) {
       }
    
       @Override
       public void onConnectionDisconnected(StringeeClient stringeeClient, boolean isReconnecting) {
       }
    
       @Override
       public void onIncomingCall(StringeeCall stringeeCall) {
       }
    
       @Override
       public void onIncomingCall2(StringeeCall2 stringeeCall2) {
       }
    
       @Override
       public void onConnectionError(StringeeClient stringeeClient, StringeeError stringeeError) {
       }
    
       @Override
       public void onRequestNewToken(StringeeClient stringeeClient) {
       }
    
       @Override
       public void onCustomMessage(String from, JSONObject msg) {
       }
    
       @Override
       public void onTopicMessage(String from, JSONObject msg) {
       }
    };

    The StringeeClient class is defined in Stringee SDK. It includes methods interacting with Stringee Server.

  2. Instantiate StringeeClient, register a StringeeConnectionListener, then call connect(token).

    client = new StringeeClient(this);
    client.addConnectionListener(connectionListener);
    client.connect(token);
    • When the client connects to Stringee Server, the onConnectionConnected(StringeeClient stringeeClient, boolean isReconnecting) method is called.
    • When the client disconnects to Stringee Server, the onConnectionDisconnected(StringeeClient stringeeClient, boolean isReconnecting) method is called
    • When the client fails to connect to Stringee Server, the onConnectionError(StringeeClient stringeeClient, StringeeError stringeeError) method is called.
    • When the token is expired, the onRequestNewToken(StringeeClient stringeeClient) is called. You will need re-generate a new token and connect again.
  3. To listen for Conversation and Message changes, register a ChangeEventListener:

    private final ChangeEventListener changeEventListener = new ChangeEventListener() {
       @Override
       public void onChangeEvent(StringeeChange change) {
       }
    };
    
    client.addChangeEventListener(changeEventListener);

Switching authenticated users

Before connecting the same StringeeClient with a token for another user, disconnect it and clear the local chat database:

client.disconnect();
client.clearDb();
client.connect(newUserToken);