From BaseX Documentation
Revision as of 14:13, 4 August 2018 by Johannes Finckh (talk | contribs)
Jump to navigation Jump to search

This page presents one of the Web Application services. It describes how to use the WebSockets API of BaseX.



There are a bunch of annotations depending to WebSockets for annotating XQuery functions. When a WebSocket message comes in, 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 '/', message to the Path '/path', ...), the module directory and its sub-directories will be traversed, and all XQuery files will be parsed for functions with WebSocket annotations. Sub-directories that include an .ignore the file will be skipped.

To speed up processing, the functions of the existing XQuery modules are automatically cached in main memory:

  • Functions will be invalidated and parsed again if the timestamp of their module changes.


A first WebSocket function is shown below:

module namespace page = 'http://basex.org/modules/web-page';
import module namespace websocket = "http://basex.org/modules/Websocket";
  function page:connect(
  )  {
        <json type="object">


  • Enable the WebSocket servlet in the web.xml. You can set here the maxIdleTime, maxTextMessageSize and maxBinaryMessageSize too.
  • If you get a message that exceeds the maxTextMessageSize/maxBinaryMessageSize or, if not set, the default messageSize of Jetty of 65 536 bytes (64 kB) then the connection will close. In this case, the websocket:error annotation will be called.
  • Annotate your specific XQuery-Functions with WebSocketAnnotations.



Called when a client WebSocket connection successfully connected to the server. The path specifies the path the client connected to.


Called when a message arrives at the server. The path specifies the path the client is connected to. The message is the message sent by the client. Could be a text-message or a binary-message.


Called when the WebSocket closes. The path specifies the path the client is connected to. The WebSocket is already closed when this annotation is called so there can be no return.


Called when an error occurred. Usually, this happens because of bad/malformed incoming packets. The path specifies the path the client is connected to. The message is the error-message. The WebSocket gets closed after an error occurred.


The value of the first parameter will be assigned to the variable specified as the second parameter. The third parameter can be a default value.


  • Http-Version -> f.e.: ```%ws:param("Http-Version", "{$version}")```
  • Origin
  • Protocol-Version
  • QueryString
  • IsSecure
  • RequestURI
  • Host
  • Sec-WebSocket-Version
  • offset -> just for binary-Messages
  • len -> just for binary-Messages