Async Channels
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’:
- Name: name that references the channel.
- Description: channel description.
- 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)
}
}