Subscribing to custom platform event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const subscription = client.Subscribe(); //client here is the grpc client. | |
//Since this is a stream, you can call the write method multiple times. | |
//Only the required data is being passed here, the topic name & the numReqested | |
//Once the system has received the events == to numReqested then the stream will end. | |
subscription.write({ | |
topicName: "/event/Your_platform_event__e", | |
numRequested: 10, | |
}); | |
//listen to new events. | |
subscription.on("data", function (data) { | |
console.log("data => ", data); | |
//data.events is an array of events. Below I am just parsing the first event. | |
//Please add the logic to handle mutiple events. | |
if (data.events) { | |
const payload = data.events[0].event.payload; | |
let jsonData = schema.fromBuffer(payload);//this schema is the same which we retreived earlier in the GetSchema rpc. | |
console.log("Event details ==> ", jsonData); | |
} else { | |
//if there are no events then every 270 seconds the system will keep publishing the latestReplayId. | |
} | |
}); | |
subscription.on("end", function () { | |
console.log("ended"); | |
}); | |
subscription.on("error", function (err) { | |
console.log("error", JSON.stringify(err)); //handle errors | |
}); | |
subscription.on("status", function (status) { | |
console.log("status ==> ", status); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment