navigation

ChannelBuilder

Namespace: Saturn
Parent: Saturn

Module with channel computation expression


Declared Types
Type Description
ChannelBuilder

Computation expression used to create channels - an controller-like abstraction over WebSockets allowing real-time, and push-based communication between server and the client The messages handled by channels should be json-encoded, in a following form: {Topic = "my topic"; Ref = "unique-message-id"; Payload = {...} }

The result of the computation expression is the IChannel instance that can be registered in the application computation expression using add_channel operation.

Example:

let browserRouter = router {
  get "/ping" (fun next ctx -> task {
    let hub = ctx.GetService<Saturn.Channels.ISocketHub>()
    match ctx.TryGetQueryStringValue "message" with
    | None ->
      do! hub.SendMessageToClients "/channel" "greeting" "hello"
    | Some message ->
      do! hub.SendMessageToClients "/channel" "greeting" (sprintf "hello, %s" message)
    return! Successful.ok (text "Pinged the clients") next ctx
   })
  }

let sampleChannel = channel {
  join (fun ctx si -> task {
    ctx.GetLogger().LogInformation("Connected! Socket Id: " + si.SocketId.ToString())
    return Ok
  })

  handle "topic" (fun ctx si msg ->
    task {
       let logger = ctx.GetLogger()
       logger.LogInformation("got message {message} from client with Socket Id: {socketId}", msg, si.SocketId)
       return ()
  })
}

let app = application {
  use_router browserRouter
  url "http://localhost:8085/"
  add_channel "/channel" sampleChannel
}
val browserRouter : obj
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val sprintf : format:Printf.StringFormat<'T> -> 'T
val sampleChannel : obj
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
val app : obj
ChannelBuilderState

Type representing internal state of the channel computation expression


Values and Functions
Name Description
channel
Signature: ChannelBuilder

Computation expression used to create channels