Async Channels

Mat|r allows communicating your mat|r device through channels. By accessing from the Model editor, which uses visual configuration, it is possible to create channels at application level which allow the communication of mat|r devices while running the app. Besides, the channels can be used with IoT devices, i.e. devices which do not run a mat|r app.

Creating and Setting Up a New Channel

To create a new channel by means of the channels configuration menu, click on the button ‘create new channel’:


 

  1. Name: name that references the channel.
  2. Description: channel description.
  3. Message Type: the message sent through the channel is the model representation in JSON format, which must be previously defined in your app by using mat|r script.

E.g.

Experience Main {
    // Add your attributes here
    String userName label("Username")
    String msg label("Mensage")
    Decision r2 action("MainContext.ruleSubscribe") label("subscribe")
    Decision r3 action("MainContext.ruleUnsubscribe") label("unsubscribe")
    Decision r4 action("MainContext.rulePublish") label("publish")
    Decision r5 action("MainContext.ruleDisconnect") label("disconnect")
    Array<String> messages value([]) as List
}
Application{
    String topicName
    OnInit{
        topicName = "mensajeria"// nombre del canal creado anteriormente 
        broker.async.initialize()
        broker.async.connect()
        broker.async.subscribe(topicName, @function(Subscriptors.handler))
    }
}

RuleContext MainContext {
    Rule ruleSubscribe{
        Main m = broker.ui.getDataSource()
        broker.async.subscribe(topicName, @function(Subscriptors.handler))
    }
    Rule rulePublish{
        Main main = broker.ui.getDataSource()
        User u = User(name:main.userName)
        Message m = Message(user:u, msg:main.msg)
        broker.async.publish(topicName, m)
    }
    
    Rule ruleUnsubscribe{
        Main m = broker.ui.getDataSource()
        broker.async.unsubscribe(topicName)
    }
    
    Rule ruleDisconnect{
        broker.async.disconnect()
    }
}

Model Message{
    String msg
    User user
}

Model User{
    String name
}

Module Subscriptors{
    void handler(Message message){
        Main m = broker.ui.getDataSource()
        m.messages.add("De " + message.user.name + ": " + message.msg)
    }
}