Warning: Reason support is experimental. We are looking for beta-tester and contributors.

How to implement a notification system?

If you want the user to receive notifications from the server (for example: "you have a new message"), first create a channel, and register it in an Eliom reference of scope `Client_process:

let channel_ref =
    (fun () -> 
       let (s, notify) = Lwt_stream.create () in
       let c = Eliom_comet.Channel.create s in
       (c, notify)

On client side, listen on this channel. To do that, execute this code (on server side) during the service that will start the client process:

let (channel, _) = Eliom_reference.Volatile.get channel_ref in
let _ =
    Lwt.async (fun () ->
        (fun v -> (* do something *))

And call function notify on the channel (from server side) when you want to notify the client.

To get back the notify functions for one user, you may want to iterate on all client process states. To do that, create a session group for each user (see How to save session data). Here we suppose that the session group name is the user_id, as a string. Then iterate on all sessions from this group, and on all client processes for each session.

For example:

let notify user_id v =
  (* We get the session group state for this user: *)
  let state =
      ~scope:Eliom_common.default_group_scope (Int64.to_string user_id)
  (* We iterate on all sessions from the group: *)
    (fun state ->
      (* We iterate on all client process states in the session: *)
        (fun state ->
          let (_, notify) = Eliom_reference.Volatile.Ext.get state channel_ref in
          notify (Some v)))

Warning: If you do not call the iterators during a request or during the initialisation phase of the Eliom module, you must provide the extra parameter ?sitedata, that you can get by calling Eliom_request_info.get_sitedata during the initialisation phase of the Eliom module.