navigation

ChannelBuilder

Namespace: Saturn
Parent: 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

Name Description
Instance Members
x.ErrorHandler(state, handler)
Signature: (state:ChannelBuilderState * handler:(HttpContext -> ClientInfo -> Message -> Exception -> Task)) -> ChannelBuilderState


CE Custom Operation: error_handler

Action executed when unhandled exception happens in the As arguments, not_found_handler action gets: current HttpContext for the request ClientInfo instance representing additional information about client sending request * Message<'a> instance representing message sent from client to the channel

x.Handle(state, topic, handler)
Signature: (state:ChannelBuilderState * topic:string * handler:(HttpContext -> ClientInfo -> Message<'a> -> Task)) -> ChannelBuilderState


CE Custom Operation: handle

Action executed when client sends a message to the channel to the given topic.

As arguments, handle action gets: current HttpContext for the request ClientInfo instance representing additional information about client sending request * Message<'a> instance representing message sent from client to the channel

x.Join(state, handler)
Signature: (state:ChannelBuilderState * handler:(HttpContext -> ClientInfo -> Task)) -> ChannelBuilderState


CE Custom Operation: join

Action executed when client tries to join the channel. You can either return Ok if channel allows join, or reject it with Rejected Typical cases for rejection may include authorization/authentication, not being able to handle more connections or other business logic reasons.

As arguments, join action gets: current HttpContext for the request ClientInfo instance representing additional information about client sending request

x.NotFoundHandler(state, handler)
Signature: (state:ChannelBuilderState * handler:(HttpContext -> ClientInfo -> Message -> Task)) -> ChannelBuilderState


CE Custom Operation: not_found_handler

Action executed when clients sends a message to the topic for which handle was not registered

As arguments, not_found_handler action gets: current HttpContext for the request ClientInfo instance representing additional information about client sending request * Message<'a> instance representing message sent from client to the channel

x.Run(state)
Signature: state:ChannelBuilderState -> IChannel

x.Terminate(state, handler)
Signature: (state:ChannelBuilderState * handler:(HttpContext -> ClientInfo -> Task)) -> ChannelBuilderState


CE Custom Operation: terminate

Action executed when client disconnects from the channel

As arguments, join action gets: current HttpContext for the request ClientInfo instance representing additional information about client sending request

x.Yield(arg1)
Signature: '?39722 -> ChannelBuilderState