When a data message is sent to a W3bstream Project, W3bstream checks if there's an event route that matches the event_type specified in the message. If there's a match, W3bstream executes the corresponding applet's handler function and passes the event's resource ID as an argument. The applet's handler function is responsible for processing the event's payload.
To access the payload, you can use the GetData function provided by the W3bstream Applet Kit in your preferred programming language. The following example demonstrates how to obtain the event payload, parse it as a JSON object, and log it to the console:
// W3bstream functions and typesimport { GetDataByRID, JSON, Log } from"@w3bstream/wasm-sdk";export { alloc } from"@w3bstream/wasm-sdk";// W3bstream handlerexportfunctionhandle_data(rid:i32):i32 {// Get the message payload from the resource idlet payload_string =GetDataByRID(rid);// Parse the payload string into a JSON objectlet payload_json =JSON.parse(payload_string) asJSON.Obj;// Log to the consoleLog("Received message: "+ payload_string);return0;}
use ws_sdk::log::log_info;use ws_sdk::stream::get_data;#[no_mangle]pubextern"C"fndata_handler(rid:i32) ->i32 {// Get the message payload from the resource idlet payload_string =String::from_utf8(get_data(rid as _)?)?;// Parse the payload string into a JSON objectlet payload:Value= serde_json::from_str(payload_stringn_str)?;// Log to the consolelog_info(&format!("Received message: `{}`", payload_string))?;return0;};
packagemainimport ("fmt""github.com/machinefi/w3bstream-wasm-golang-sdk/log""github.com/machinefi/w3bstream-wasm-golang-sdk/stream""github.com/tidwall/gjson")// main is required for TinyGo to compile to Wasm.funcmain() {}//export startfunc_start(rid uint32) int32 {// Get the message payload from the resource id message, err := stream.GetDataByRID(rid)// Parse the payload string into a JSON object res := gjson.Parse(message);// Log to the console message_string :=string(message) log.Log("Message received: "+ message_string)return0}