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

Send result to another service

Chain multiple functions together

PreviousContextNextInput and output

Last updated 4 years ago

Was this helpful?

This page is outdated. Please .

A common use case of serverless functions is to act as the bridge between several web services or messaging queues. It receives a request from a source, and then send the return value onto the next service.

The way to accomplish that is to return a JSON string from the function. If the JSON object contains a callback object, the FaaS would strip it from the return value, and then send the rest of the return value to the HTTP endpoint defined in the callback.

In the following example, the say() function returns a JSON callback, which requests SendGrid to send the hello messages as an email. The Rust function is as follows.

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn say(context: &str, s: &str) -> String {
  let r = String::from("hello ");
  let ret = "
    {
      'callback': {
        'method': 'POST',
        'hostname': 'api.sendgrid.com',
        'port': 443,
        'path': '/v3/mail/send',
        'headers': {
          'Content-Type': 'application/json',
          'authorization': 'Bearer AUTH_TOKEN'
        },
        'maxRedirects': 20
      },
      'personalizations': {
        [{
          'to':[{'email':'TO_EMAIL','name':''}],
          'subject':'SUBJECT'
        }],
        'from':{'email':'FROM_EMAIL','name':''}
      }
    }
  ";
  
  let ret = ret.replace("AUTH_TOKEN", "auth_token_123");
  let ret = ret.replace("TO_EMAIL", "alice@secondstate.io");
  let ret = ret.replace("SUBJECT", &(r + &s));
  let ret = ret.replace("FROM_EMAIL", "dev@developer.com");
  return ret;
}

The callback object in the return value is as follows. It conforms to the Node.js request options specification.

{
  'method': 'POST',
  'hostname': 'api.sendgrid.com',
  'port': 443,
  'path': '/v3/mail/send',
  'headers': {
    'Content-Type': 'application/json',
    'authorization': 'Bearer <<YOUR_API_KEY>>'
  },
  'maxRedirects': 20
}

The HTTP body sent to SendGrid is as follows.

{'personalizations':
  [{
    'to':[{'email':"dev@example.com","name":""}],
    'subject':'hello email'
  }],
  'from':{'email':'alice@secondstate.io','name':''}
}

The recipient email address and auth token are currently hardcoded in the Rust source code. But you can use the stateful context in the previous article to configure them! Just set a JSON configuration object in the state. Try it!

You can see that the callback could direct the result from one function to another, and hence chaining multiple functions together.

visit here for the most up-to-date content