On this page:
4.1 RPCs, Events, State, Records, and Enums
rivet-type
Void
Bool
Int64
String
Bytes
List
state-info
record-info
enum-info
define-rpc
define-record
record-ref
define-enum
enum-case
define-event
define-state
emit-event!
state-ref
state-set!
4.2 Serving an Embedded Connection
serve
serve-fds
rpc-schema
event-schema
state-schema
record-schema
enum-schema

4 Application Backend🔗ℹ

 (require rivet) package: rivet

4.1 RPCs, Events, State, Records, and Enums🔗ℹ

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.

struct

(struct record-info (name field-names field-types))

  name : symbol?
  field-names : list?
  field-types : list?
Represents the ordered schema of a named Record. Applications normally create one with define-record instead of calling the constructor directly.

struct

(struct enum-info (name cases))

  name : symbol?
  cases : list?
Represents the ordered cases of a named Enum. Applications normally create one with define-enum 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, C++, and Kotlin clients. Supported schema types are String, Int64, Bool, Bytes, Void, Any, (List type), (Optional type), and names introduced by define-record or define-enum. The result is validated before it is placed on the wire.

syntax

(define-record name ([field : field-type] ...))

Defines a constructor named name and registers an ordered, typed Record schema. Records generate Swift and C++ structs plus Kotlin data classes and use an RVT1 List in field declaration order.

procedure

(record-ref value field) → any/c

  value : any/c
  field : (or/c symbol? string?)
Returns a named field from a value constructed by define-record.

syntax

(define-enum name (case ...))

Defines a constructor named name and registers a closed, ordered set of cases. The constructor accepts a case symbol or string. Generated Swift uses a raw-value enum; generated C++ uses enum class; generated Kotlin uses an enum class with the wire name; RVT1 carries the stable case name as a String.

procedure

(enum-case value) → symbol?

  value : any/c
Returns the case symbol from a value constructed by define-enum.

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)

4.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.

procedure

(record-schema) → list?

Returns the registered Record schema.

procedure

(enum-schema) → list?

Returns the registered Enum schema.