Receiving data

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 types
import { GetDataByRID, JSON, Log } from "@w3bstream/wasm-sdk";
export { alloc } from "@w3bstream/wasm-sdk";

// W3bstream handler
export function handle_data(rid: i32): i32 {
  // Get the message payload from the resource id
  let payload_string = GetDataByRID(rid);
  // Parse the payload string into a JSON object
  let payload_json = JSON.parse(payload_string) as JSON.Obj;
  // Log to the console
  Log("Received message: " + payload_string);
  return 0;

Last updated