Create a Channel
The example-5 script authenticates an Integration Service client to manage Identities using the Admin identity created in example-0 and then performs the following tasks:
You can run the example with the following command:
- Java
- Node.js
Create Channel
You can create a channel using the following script:
- Java
- Node.js
Map<String, String> topics = new HashMap<String, String>();
topics.put("type", "example-data");
topics.put("source", "data-creator");
List<Map<String, String>> allTopics = new ArrayList<Map<String, String>>();
allTopics.add(topics);
// Create a new channel for example data
JSONObject newChannelDetails = channelClient.create(null, allTopics, null, null, null);
// The channel address is used to read and write to channels
String channelAddress = newChannelDetails.getString("channelAddress");
System.out.println(channelAddress);
// The owner creates a channel where he/she want to publish data of type 'example-data'.
const { channelAddress } = await ownerClient.create({
name: `Channel-${Math.ceil(Math.random() * 100000)}`,
topics: [{ type: 'example-data', source: 'data-creator' }],
});
Write on a Channel
You can write on a channel using the following script:
- Java
- Node.js
// Writing 3 data packets to channel
for (int i = 0; i < 3; i++) {
System.out.println("writing data to channel no " + i);
channelClient.write(channelAddress, "log", null, new JSONObject().put("log", "This is log number " + i));
}
await channel.write(channelAddress, {
type: 'log',
created: new Date().toISOString(),
payload: {
log: `This is log file ...`,
},
});
Read from a Channel
You can read from a channel using the following script:
- Java
- Node.js
List<ChannelData> datas = channelClient.read(channelAddress, null, null, null, null, null);
for (ChannelData data : datas) {
System.out.println(data.toString());
}
const channelData = await channel.read(channelAddress);
console.log('Read from channel:');
channelData.forEach((data) => {
console.log(data.log);
});