Second State
  • What is Second State?
  • Server-side WebAssembly
    • Why?
      • WebAssembly vs native code
    • Getting started
      • VS Codespaces
      • The ssvmup tool
    • Rust functions in Node.js
    • Access system resources
      • WASI
      • The storage interface
      • The inference interface
    • Examples and use cases
      • Encryption and decryption
      • Machine learning
      • Artificial intelligence
    • SSVM Performance
  • Function as a Service
    • Getting started
      • Context
      • Send result to another service
    • Input and output
      • JSON argument
      • Binary argument
      • Multiple arguments
      • Argument from a URL
      • JSON return value
      • Binary return value
      • Redirect return value
    • Stateful execution
  • Related technologies
    • Deno, Rust and WebAssembly
    • A Rusty hello world
    • Rust and WebAssembly
    • WebAssembly in the browser
    • Access JavaScript from Rust
    • How to Learn Rust Without Installing Any Software
    • How to Publish a no-code website in 10 minutes
Powered by GitBook
On this page

Was this helpful?

  1. Function as a Service
  2. Getting started

Context

Give the function a context

PreviousGetting startedNextSend result to another service

Last updated 4 years ago

Was this helpful?

This page is outdated. Please .

The first argument of any function must be a &str. You can use it to pass a context value to every function call. For example, we can set a context string for wasm ID 123, which is the hello world wasm example we just created. The context specifies that the function should say hello in emoji.

curl --location --request PUT 'https://rpc.ssvm.secondstate.io:8081/api/state/123' \
--header 'Content-Type: text/plain' \
--data-raw 'emoji'

The Rust function now looks like this.

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn say(context: &str, s: &str) -> String {
  if context == "emoji" {
    let r = String::from("👋 ");
    return r + &s;
  } else {
    let r = String::from("hello ");
    return r + &s;
  }
}

The next time you run it, it says hello based on the context.

$ curl --location --request POST 'https://rpc.ssvm.secondstate.io:8081/api/run/123/say' \
--header 'Content-Type: text/plain' \
--data-raw 'Second State FaaS'

The response is as follows.

👋 Second State FaaS

We have just used a simple string as the context. For more complex use cases, the context could be a JSON object. You can set the state to the entire JSON string. The function is responsible for parsing the JSON string argument and makes sense of it.

You can also store the Second State VM storage ID in the stateful context. That enables stateful functions that can update and maintain its internal states across execution runs. We will cover that use case in a later tutorial.

visit here for the most up-to-date content