aws-amplify#PubSub JavaScript Examples

The following examples show how to use aws-amplify#PubSub. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: App.js    From aws-iot-enhanced-custom-authorizer-demo with MIT No Attribution 6 votes vote down vote up
componentDidMount(){
    PubSub.subscribe(mqtt_topic).subscribe({
        next: data => {
          data.value.client_received_at = new Date()
          console.log(`Message received: ${JSON.stringify(data.value)} \nRaw data: ${JSON.stringify(data)}`)

          this.setState(prevState => ({
            connectionState: `Connected and Subscribed to topic '${mqtt_topic}'; Messages Received`,
            messages: [...prevState.messages, data.value]
          }))
        },
        error: error => {
          console.log(JSON.stringify(error, null, 2))
          this.setState(prevState => ({
            connectionState: `Failed to subscribe to topic '${mqtt_topic}' on endpoint ${mqtt_host}: ${error.error.errorMessage}`
          }))
        },
        close: () => {
          this.setState({ connectionState: `Connection Closed` })
        },
    })


    this.setState({ connectionState: `Connected and Subscribed to topic '${mqtt_topic}'; Awaiting Messages...` })
  }