Changes

Jump to navigation Jump to search
350 bytes added ,  18:38, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
==Protocol==
Use WebSockets if you have to exchange data with a high frequency or if you have to send messages from the server to the client without techniques like [polling https://en.wikipedia.org/wiki/Polling_(computer_science)polling]. In contrast to REST, WebSockets use a single URL for the whole communication.
The WebSocket protocol was standardized in [https://tools.ietf.org/html/rfc6455 RFC 6455] by the IETF. After an initial HTTP request, all communication takes place over a single TCP connection. Unlike the HTTP protocol, a connection will be kept alive, and a server can send unsolicited data to the client.
There are a bunch of annotations depending to WebSockets for annotating XQuery functions. When a WebSocket message arrives at the server, an XQuery function will be invoked that matches the constraints indicated by its annotations.
If a WebSocket function is requested (like connecting to the path <code>/</code>, sending a message to the path <code>/path</code>, …), the module directory and its sub-directories subdirectories will be traversed, and all [[XQuery Extensions#Suffixes|XQuery files]] will be parsed for functions with WebSocket annotations. Sub-directories Subdirectories that include an {{Code|.ignore}} file will be skipped.
To speed up processing, the functions of the existing XQuery modules are automatically cached in main memory. For further information on cache handling, check out the [[RESTXQ#Introduction|RESTXQ introduction]].
To tag functions as WebSocket functions you have to use [[XQuery 3.0#Annotations|annotations]]. The annotation is written after the keyword ''declare'' and before the keyword ''function''. For the context of WebSockets there are some annotations listed below. Functions which are annotated with a WebSocket annotation will be called if the appropriate event occurs. For example, the function annotated with <code>ws:connect('/')</code> will be executed if a client establishes a connection with the WebSocket root path (which is, by default, <code>ws/</code>). By using annotations, it’s easy to provide an API for your WebSocket connection. You just have to specify what to do when a WebSocket Event occurs, annotate it with the corresponding annotation and the Servlet will do the rest for you.
==%ws:connect(path)==
Called directly after a successful WebSocket handshake. The <code>path</code> specifies the path which a client is connected to.: <pre lang='xquery'>declare %ws:connect('/') function local:connect() { };</pre>
You can specify here how to handle your users, e. g. save a name as a WebSocket attribute. Furthermore, you can check header parameters for validity.
==%ws:message(path, message)==
Called when a client message arrives at the server. The <code>path</code> specifies the path which a client is connected to. The <code>message</code> string contains the name of the variable to which the message will be bound:
<pre classlang="brush:'xquery"'>
declare %ws:message('/', '{$info}') function local:message($info) { };
</pre>
The value will be of type <code>xs:string</code> or <code>xs:base64Binary</code>. As there is no fixed message protocol, the client needs to take care of the message syntax.
==%ws:error(path, message)==
Called when an error occurs. The <code>path</code> specifies the path which a client is connected to. The <code>message</code> string contains the name of the variable to which the message will be bound:
<pre classlang="brush:'xquery"'>
declare %ws:error('/', '{$error}') function local:error($error) { };
</pre>
Usually, errors happen because of bad/malformed incoming packets. The WebSocket connection gets closed after the error handling.
==%ws:close(path)==
Called when the WebSocket closes. The <code>path</code> specifies the path which a client is connected to.: <pre lang='xquery'>declare %ws:close('/') function local:connect() { };</pre>
The WebSocket is already closed when this annotation is called so there can be no return.
==%ws:header-param(name, variable[, default])==
For accessing connection-specific properties like the HTTP version. The value will be bound to the specified <code>variable</code>. If the property has no value, an optional <code>default</code> value will be assigned instead:
<pre classlang="brush:'xquery"'>
declare
%ws:close('host', '{$host}')
If you get the <code>[basex:ws] WebSocket connection required</code> error, you may be attempting to call WebSocket functions from a non-WebSocket context. If you use a proxy server, check in the configuration if WebSockets are enabled.
=Examples= ==Basic Example==
The following chapter explains how to create a simple basic web application with WebSockets. You can find another example in the BaseX source code.
For establishing a connection to the WebSocket server, it is necessary that the server provides at least one function annotated with a WebSocket annotation. Let’s start by using the annotation <code>%ws:connect('/')</code>. In the connect function, a bidirectional communication with the client can be initialized: attributes such as the id and name of a client can be set, or a welcome message can be emitted to other connected users, and so on.
<pre classlang="brush:'xquery"'>
declare
%ws:connect('/')
The connect function is sufficient for creating the persistent client/server connection. In order to something sensible with the connection, you should implement a function annotated with <code>%ws:message("/")</code>:
<pre classlang="brush:'xquery"'>import module namespace ws = 'http://basex.org/modules/ws';
declare
The following client-side code demonstrates a basic application of the WebSocket connection:
<pre classlang="brush:javajavascript">var ws = new WebSocket("ws://localhost:89848080/ws");
ws.onmessage = function(event) {
</pre>
The <code>send</code> function can be called to pass on a string to the server. There are no heart-beats in this example. This means that the connection is terminated if nothing happens for 5 minutes (estandard timeout). gIt will also be closed if you send a message that exceeds the standard text size.  ==Chat Application== In the full distributions of BaseX, you will find a little self-contained chat message) to the serverapplication that demonstrates how WebSockets can be used in practice=Changelog=
There are no heartbeats in this exampleWebSockets werre introduced with Version 9. This means that the connection is terminated if nothing happens for 5 minutes (standard timeout). It will also be closed if you send a message that exceeds the standard text size1.
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu