Difference between revisions of "WebSocket Module"
m (Text replacement - "<br />" to "<br/>") |
|||
Line 14: | Line 14: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:id() as xs:string</pre> |
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 28: | Line 28: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:ids() as xs:string*</pre> |
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 39: | Line 39: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:path( |
+ | $id as xs:string | ||
+ | ) as xs:string</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 53: | Line 55: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:close( |
+ | $id as xs:string | ||
+ | ) as empty-sequence()</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 69: | Line 73: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:send( |
+ | $message as item() | ||
+ | $ids as xs:string* | ||
+ | ) as empty-sequence()</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 83: | Line 90: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:broadcast( |
+ | $message as xs:anyAtomicType | ||
+ | ) as empty-sequence()</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 94: | Line 103: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:emit( |
+ | $message as xs:anyAtomicType | ||
+ | ) as empty-sequence()</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 105: | Line 116: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:eval( |
+ | $query as xs:anyAtomicType | ||
+ | $bindings as map(*)? := () | ||
+ | $options as map(*)? := () | ||
+ | ) as xs:string</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 135: | Line 150: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:get( |
+ | $id as xs:string | ||
+ | $name as xs:string | ||
+ | $default as item()* := () | ||
+ | ) as item()*</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 149: | Line 168: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:set( |
+ | $id as xs:string | ||
+ | $name as xs:string | ||
+ | $value as item()* | ||
+ | ) as empty-sequence()</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 163: | Line 186: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>ws:delete( |
+ | $id as xs:string | ||
+ | $name as xs:string | ||
+ | ) as empty-sequence()</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' |
Revision as of 13:02, 9 March 2023
This XQuery Module contains functions for accessing specific WebSocket functions. This module is mainly useful in the context of WebSockets.
Contents
Conventions
- The module will be available if the
basex-api
library is found in the classpath. This is the case if you use one of the complete distributions of BaseX (zip, exe, war). - All functions and errors are assigned to the
http://basex.org/modules/ws
namespace, which is statically bound to thews
prefix. - As sessions are side-effecting operations, all functions are flagged as non-deterministic. As a result, some query optimizations will be suppressed.
General Functions
ws:id
Signatures | ws:id() as xs:string |
Summary | Returns the ID of the current WebSocket. |
Errors | not-found : No WebSocket with the specified id exists.
|
ws:ids
Signatures | ws:ids() as xs:string* |
Summary | Returns the ids of all currently registered WebSockets. |
ws:path
Signatures | ws:path( $id as xs:string ) as xs:string |
Summary | Returns the path of the WebSocket with the specified $id .
|
Errors | not-found : No WebSocket with the specified id exists.
|
ws:close
Signatures | ws:close( $id as xs:string ) as empty-sequence() |
Summary | Closes the connection of the WebSocket with the specified $id .
|
Errors | not-found : No WebSocket with the specified id exists.
|
Sending Data
ws:send
Signatures | ws:send( $message as item() $ids as xs:string* ) as empty-sequence() |
Summary | Sends a $message to the clients with the specified $ids . Ids that cannot be assigned to clients will be ignored. The message will be handled as follows:
|
ws:broadcast
Signatures | ws:broadcast( $message as xs:anyAtomicType ) as empty-sequence() |
Summary | Broadcasts a $message to all connected clients except to the caller. Invocations of this convenience function are equivalent to ws:send($message, ws:ids()[. != ws:id()]) . See ws:send for more details on the message handling.
|
ws:emit
Signatures | ws:emit( $message as xs:anyAtomicType ) as empty-sequence() |
Summary | Emits a $message to all connected clients. Invocations of this function are equivalent to ws:send($message, ws:ids()) . See ws:send for more details on the message handling.
|
ws:eval
Signatures | ws:eval( $query as xs:anyAtomicType $bindings as map(*)? := () $options as map(*)? := () ) as xs:string |
Summary | Schedules the evaluation of the supplied $query and returns the result to the calling WebSocket client. The query can be a URI or a string, and variables and context items can be declared via $bindings (see xquery:eval for more details). The following $options can be supplied:
Query scheduling is recommendable if the immediate query execution might be too time consuming and lead to a timeout. |
Errors | overflow : Query execution is rejected, because too many jobs are queued or being executed. id : The specified id is invalid or has already been assigned.
|
Examples |
<syntaxhighlight lang="xquery"> declare %ws:message('/tasks', '{$message}') function local:message($message) { ws:eval('prof:sleep(10000), "Your message has been processed."') }; </syntaxhighlight> |
WebSocket Attributes
ws:get
Signatures | ws:get( $id as xs:string $name as xs:string $default as item()* := () ) as item()* |
Summary | Returns the value of an attribute with the specified $name from the WebSocket with the specified $id . If the attribute is unknown, an empty sequence or the optionally specified $default value will be returned instead.
|
Errors | not-found : No WebSocket with the specified id exists.
|
ws:set
Signatures | ws:set( $id as xs:string $name as xs:string $value as item()* ) as empty-sequence() |
Summary | Returns the specified value of the attribute with the specified $name from the WebSocket with the specified $id .
|
Errors | not-found : No WebSocket with the specified id exists.
|
ws:delete
Signatures | ws:delete( $id as xs:string $name as xs:string ) as empty-sequence() |
Summary | Deletes an attribute with the specified $name from the WebSocket with the specified $id .
|
Errors | not-found : No WebSocket with the specified id exists.
|
Examples
Example 1
<syntaxhighlight lang="xquery"> import module namespace ws = "http://basex.org/modules/ws";
declare
%ws:connect('/')
function local:connect() as empty-sequence() {
let $id := ws:id() let $message := json:serialize(map { 'type': 'Connect', 'id': $id }) return ws:broadcast($message)
}; </syntaxhighlight>
Explanation:
- The function has a
%ws:connect
annotation. It gets called if a client successfully creates a WebSocket connection to the path/
(check out WebSockets for further information). - A JSON response is generated, which contains the new client id and a
Connect
string. - This response will be sent to all other connected clients.
Example 2
<syntaxhighlight lang="xquery"> import module namespace ws = "http://basex.org/modules/ws";
declare
%ws:message('/', '{$message}')
function local:message(
$message as xs:string
) as empty-sequence() {
let $message := json:serialize(map { 'message': $message }) return ws:emit($message)
}; </syntaxhighlight>
Explanation:
- The function has a
%ws:message
annotation. It gets called if a client sends a new message. - A JSON response is generated, which contains the message string.
- This response will be sent to all connected clients (including the calling client).
Errors
Code | Description |
---|---|
not-found
|
No WebSocket with the specified id exists. |
Changelog
- Version 9.2
- Added:
ws:eval
This module was introduced with Version 9.1.