async initSession()

in src/chat.js [57:102]


  async initSession() {
    const user = await Auth.currentAuthenticatedUser();
    const authId = user.attributes.sub;
    const logger = new ConsoleLogger('SDK', LogLevel.INFO);
    const endpoint = await chimeApi.getMessagingSessionEndpoint();
    const userArn = chimeApi.createMemberArn(authId);
    const userName = user.username;
    const sessionId = null;
    const creds = await Auth.currentCredentials();
    const chime = new Chime({
      region: 'us-east-1',
      credentials: await Auth.essentialCredentials(creds)
    });
    const configuration = new MessagingSessionConfiguration(userArn, sessionId, endpoint.Endpoint.Url, chime, AWS);
    const messagingSession = new DefaultMessagingSession(configuration, logger);
    const observer = {
      messagingSessionDidStart: () => {
        console.log('Session started');
      },
      messagingSessionDidStartConnecting: reconnecting => {
        if (reconnecting) {
          console.log('Start reconnecting');
        }
        else {
          console.log('Start connecting');
        }
      },
      messagingSessionDidStop: event => {
        console.log(`Closed: ${event.code} ${event.reason}`);
      },
      messagingSessionDidReceiveMessage: message => {
        console.log(`Receive message type ${message.type}`);
        const msg = JSON.parse(message.payload)
        console.log(msg);
        if (msg !== null && msg.Sender.Name !== userName) {
          this.addMessageList(msg, msg.Content, "incoming");
        }
      }
    };

    messagingSession.addObserver(observer);
    messagingSession.start();
    this.setState({
      messagingSession: messagingSession,
    })
  }