On this page:
3.1 RPCs, Events, and State
rivet-type
Void
Bool
Int64
String
Bytes
List
state-info
define-rpc
define-event
define-state
emit-event!
state-ref
state-set!
3.2 Serving an Embedded Connection
serve
serve-fds
rpc-schema
event-schema
state-schema

3 Application Backend🔗ℹ

 (require rivet) package: rivet

3.1 RPCs, Events, and State🔗ℹ

struct

(struct rivet-type (name))

  name : symbol?
Represents a public schema type descriptor.

value

Void : rivet-type?

value

Bool : rivet-type?

value

List : rivet-type?

struct

(struct state-info (name type cell lock))

  name : symbol?
  type : any/c
  cell : box?
  lock : semaphore?
Represents a registered shared state value. Applications normally create one with define-state instead of calling the constructor directly.

syntax

(define-rpc (name [arg : type] ... : result-type) body ...)

Defines a Racket procedure named name and registers it as an RPC for generated Swift and C++ clients. Supported schema types are String, Int64, Bool, Bytes, Void, Any, (List type), and (Optional type). The result is validated before it is placed on the wire.

syntax

(define-event name : type)

Registers an event and defines name as a one-argument procedure that emits a validated event value while a Rivet server is active. Omitting the type uses Any. Event payloads cannot have type Void.

syntax

(define-state name : type initial-value)

Creates and registers a shared state value. Native clients can read and set the state through generated APIs, and successful changes emit Rivet’s reserved state event. State values cannot have type Void.

procedure

(emit-event! name value) → void?

  name : (or/c symbol? string?)
  value : any/c
Emits an already registered event value on the active server connection. The procedure reports an error when no server is active on the current thread.

procedure

(state-ref state) → any/c

  state : state-info?
Returns the current state value.

procedure

(state-set! state value) → void?

  state : state-info?
  value : any/c
Validates and atomically commits a state value, then admits its native event in the same update order.

(require rivet)
 
(define-rpc (greet [name : String] : String)
  (string-append "Hello, " name))
 
(define-event notification : String)
(define-state signed-in? : Bool #f)

3.2 Serving an Embedded Connection🔗ℹ

procedure

(serve in    
  out    
  [#:max-pending-requests max-pending-requests    
  #:max-outgoing-frames max-outgoing-frames]) → void?
  in : input-port?
  out : output-port?
  max-pending-requests : exact-positive-integer? = 1024
  max-outgoing-frames : exact-positive-integer? = 64
Runs the RVT1 backend over binary ports. Requests execute concurrently within the configured bound, while outgoing responses and events use bounded backpressure.

procedure

(serve-fds input-fd    
  output-fd    
  [#:max-pending-requests max-pending-requests    
  #:max-outgoing-frames max-outgoing-frames]) → void?
  input-fd : exact-integer?
  output-fd : exact-integer?
  max-pending-requests : exact-positive-integer? = 1024
  max-outgoing-frames : exact-positive-integer? = 64
Adapts native file descriptors to binary ports and calls serve.

procedure

(rpc-schema) → list?

Returns the registered RPC schema used by code generation.

procedure

(event-schema) → list?

Returns the registered event schema.

procedure

(state-schema) → list?

Returns the registered state schema.