chat/util  library module

Summary

Simple WebSocket chat. Utility functions.
Tags

Author: BaseX Team 2005-21, BSD License

__source : chat-util.xqm

Related documents
ViewDescriptionFormat
xqdocxqDoc xml file from the source modulexml
xqparsexqparse xml file from the source modulexml

Imports

This module is imported by 2 modules. It imports 2 modules.

imports
this
imports
http://basex.org/modules/session
http://basex.org/modules/ws

Variables

3.1 $chat-util:id

Summary
User id (bound to sessions and WebSockets).
Type
xs:string

Functions

4.1 chat-util:close

Arities: chat-util:close#1

Summary
Closes all WebSocket connections from the specified user.
Signature
chat-util:close ( $name as xs:string )  as empty-sequence()
Parameters
  • name as xs:string user name
Return
  • empty-sequence()
Invokes 3 functions from 1 modules
  • {http://basex.org/modules/ws}close#1
  • {http://basex.org/modules/ws}get#2
  • {http://basex.org/modules/ws}ids#0
Invoked by 0 functions from 0 modules
    Source ( 7 lines)
    function chat-util:close(
      $name  as  xs:string
    ) as empty-sequence() {
      for $id in ws:ids()
      where ws:get($id, $chat-util:id) = $name
      return ws:close($id)
    }

    4.2 chat-util:message

    Arities: chat-util:message#2

    Summary
    Sends a message to all clients, or to the clients of a specific user.
    Signature
    chat-util:message ( $text as xs:string, $to as xs:string? )  as empty-sequence()
    Parameters
    • text as xs:string text to be sent
    • to as xs:string? receiver of a private message (optional)
    Return
    • empty-sequence()
    Invokes 9 functions from 2 modules
    • {http://basex.org/modules/ws}get#2
    • {http://basex.org/modules/ws}id#0
    • {http://basex.org/modules/ws}ids#0
    • {http://basex.org/modules/ws}send#2
    • {http://www.w3.org/2005/xpath-functions}boolean#1
    • {http://www.w3.org/2005/xpath-functions}current-time#0
    • {http://www.w3.org/2005/xpath-functions}format-time#2
    • {http://www.w3.org/2005/xpath-functions}not#1
    • {http://www.w3.org/2005/xpath-functions}serialize#1
    Invoked by 1 functions from 1 modules
    Source ( 13 lines)
    function chat-util:message(
      $text  as xs:string,
      $to    as xs:string?
    ) as empty-sequence() {
      let $ws-ids := ws:ids()[not($to) or ws:get(., $chat-util:id) = $to]
      return ws:send(map {
        'type': 'message',
        'text': serialize($text),
        'from': ws:get(ws:id(), $chat-util:id),
        'date': format-time(current-time(), '[H02]:[m02]:[s02]'),
        'private': boolean($to)
      }, $ws-ids)
    }

    4.3 chat-util:users

    Arities: chat-util:users#0

    Summary
    Sends a users list (all, active) to all registered clients.
    Signature
    chat-util:users ( )  as empty-sequence()
    Return
    • empty-sequence()
    Invokes 6 functions from 3 modules
    • {http://basex.org/modules/user}list#0
    • {http://basex.org/modules/ws}emit#1
    • {http://basex.org/modules/ws}get#2
    • {http://basex.org/modules/ws}ids#0
    • {http://www.w3.org/2005/xpath-functions}distinct-values#1
    • {http://www.w3.org/2005/xpath-functions}sort#1
    Invoked by 2 functions from 1 modules
    Source ( 10 lines)
    function chat-util:users() as empty-sequence() {
      ws:emit(map {
        'type': 'users',
        'users': array { sort(user:list()) },
        'active': array { distinct-values(
          for $id in ws:ids()
          return ws:get($id, $chat-util:id)
        )}
      })
    }

    Namespaces

    The following namespaces are defined:

    PrefixUri
    chat-utilchat/util
    sessionhttp://basex.org/modules/session
    wshttp://basex.org/modules/ws

    6 RestXQ

    None

    Source Code

    (:~
     : Simple WebSocket chat. Utility functions.
     : @author BaseX Team 2005-21, BSD License
     :)
    module namespace chat-util = 'chat/util';
    
    import module namespace session = 'http://basex.org/modules/session';
    import module namespace ws = 'http://basex.org/modules/ws';
    
    (:~ User id (bound to sessions and WebSockets). :)
    declare variable $chat-util:id := 'id';
    
    (:~
     : Sends a users list (all, active) to all registered clients.
     :)
    declare function chat-util:users() as empty-sequence() {
      ws:emit(map {
        'type': 'users',
        'users': array { sort(user:list()) },
        'active': array { distinct-values(
          for $id in ws:ids()
          return ws:get($id, $chat-util:id)
        )}
      })
    };
    
    (:~ 
     : Sends a message to all clients, or to the clients of a specific user.
     : @param  $text  text to be sent
     : @param  $to    receiver of a private message (optional)
     :)
    declare function chat-util:message(
      $text  as xs:string,
      $to    as xs:string?
    ) as empty-sequence() {
      let $ws-ids := ws:ids()[not($to) or ws:get(., $chat-util:id) = $to]
      return ws:send(map {
        'type': 'message',
        'text': serialize($text),
        'from': ws:get(ws:id(), $chat-util:id),
        'date': format-time(current-time(), '[H02]:[m02]:[s02]'),
        'private': boolean($to)
      }, $ws-ids)
    };
    
    (:~
     : Closes all WebSocket connections from the specified user.
     : @param  $name  user name
     :)
    declare function chat-util:close(
      $name  as  xs:string
    ) as empty-sequence() {
      for $id in ws:ids()
      where ws:get($id, $chat-util:id) = $name
      return ws:close($id)
    };