Hello World!
Notice: W3bstream is under development.
The current documentation refers to W3bstream 1.0.0-rc2
SDKs to build W3bstream applets are available for the following languages:
When creating an applet for W3bstream, you must export at least one function inside the resulting WASM module, to serve as an event handler in W3bstream.
This handler can then be linked to a W3bstream event by configuring a strategy in the project's event strategies configuration.
💡 Learn more
When deploying an applet to a W3bstream project, W3bstream creates a default event strategy that connects any W3bstream event to a default
_start()
handler function with the following signature:AssemblyScript
export function start(rid: i32): i32
Go
//export start
func _start(event_id uint32) int32
Rust
#[no_mangle]
pub extern "C" fn start(event_id: i32) -> i32
C++
#EMSCRIPTEN_KEEPALIVE uint32_t _start(uint32_t event_id)
W3bstream provides a text console for applets to output text to, which can be accessed through the Log function of the SDK:
AssemblyScript
Golang
import { Log } from "../../assembly/index";
...
Log("Some text");
import "github.com/machinefi/w3bstream-wasm-golang-sdk/log"
...
log.Log("Some text");
Creating a “Hello World!” example for W3bstream is now very simple:
AssemblyScript
Go
Let's start with creating a new folder for the project
mkdir helloworld && cd helloworld
Initialize the project with
npm
npm init -y
Install the AssemblyScript compiler
npm install --save-dev assemblyscript
Scaffold a new AssemblyScript project:
npx asinit .
Add the W3bstream AssemblyScript SDK package:
npm install @w3bstream/wasm-sdk
edit the
scripts
field in package.json
and replace it with the following:"scripts": {
"asbuild:release": "asc assembly/index.ts --use abort=assembly/index/abort --target release",
},
Edit
assembly/index.ts
, paste the following code and save it:import { Log } from "@w3bstream/wasm-sdk";
// Export alloc() is needed for w3bstream vm
export { alloc } from "@w3bstream/wasm-sdk";
export function start(rid: i32): i32 {
Log("Hello World!");
return 0;
}
// The abort method will be exposed inside w3bstream
// in a later release. For now, you can use it to log errors:
export function abort(
message: string | null,
fileName: string | null,
lineNumber: u32,
columnNumber: u32
): void {
if (message == null) message = "unknown error";
if (fileName == null) fileName = "unknown file";
Log("ABORT: " + message
+ " at " + fileName
+ ":" + lineNumber.toString()
+ ":" + columnNumber.toString());
}
Finally, build the wasm module with:
npm run asbuild
At the end of the building process, the W3bstream module can be found in
build/release.wasm
Let's start with creating a new folder for the project
mkdir helloworld && cd helloworld
Initialize the project with
go mod
go mod init helloworld
create
helloworld.go
with the current code and save it:opackage main
import "github.com/machinefi/w3bstream-wasm-golang-sdk/log"
// main is required for TinyGo to compile to WASM
func main() {}
//export start
func _start(rid uint32) int32 {
log.Log("Hello World!")
return 0
}
install required imports (in this case, the W3bstream Go SDK):
go mod tidy
tinygo build -o helloworld.wasm -scheduler=none --no-debug -target=wasi helloworld.go
At the end of the building process, the file
helloworld.wasm
can be found in the current folder.Once you built the Hello World! example as a WASM module, you can upload and deploy it to the W3bstream node as explained in the Get Started section of this documentation:
then use the W3bstream Studio to send any message to your project (make sure the applet is running!):
finally, check the log of your W3bstream node: you should see the "Hello World!" string printed:
...
// W3bstream calling the "start" handler upon message received
{
"@lv": "info",
"@prj": "srv-applet-mgr",
"@ts": "20221102-140006.340Z",
"msg": "call handler:start"
}
// This is the log from the start function
{
"@applet": "MyApplet",
"@lv": "info",
"@namespace": "HelloWorldProject",
"@prj": "srv-applet-mgr",
"@src": "wasm",
"@ts": "20221102-140006.340Z",
"msg": "Hello World!"
}
Last modified 1mo ago