From BaseX Documentation
Revision as of 14:13, 4 October 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. WebSocket is a communication protocol for providing full-duplex communication.


WebSocket 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. In contrast to REST the WebSockets use a single URL for the whole communication.

The WebSocket protocol was standardized in 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.

For establishing a WebSocket connection, a handshake request is sent by the client. The web server returns a handshake response. If the handshake is successful, the persistent connection will be open until the client or the server closes it, an error occurs or a timeout happens. It is possible to transmit all kind of data, binary or text. The BaseX-WebServer handles the handshake completely. You just have to define some limits of the connection in the web.xml and specify functions for WebSocket events like onConnect and onMessage.

Notice that there is no specification of a message-protocol. The WebSocket protocol just specifies the message-architecture but not how the payload of the messages is formatted. To agree on a format between the server and the client one can use subprotocols.

Some older browsers don't support the WebSocket protocol. Therefore you can use fallback options like Ajax. JavaScript clientlibarys like SockJs can be used for building client applications. The library takes care of how to establish the real-time connection. If the WebSocket protocol isn't supported it uses polling. You have to provide server functions for the fallback solutions if you have to support fallbacks.


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 /, sending a 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 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.


  • The WebSocket servlet has to be enabled in the web.xml (which is the default case). You can specify the maxIdleTime, maxTextMessageSize and maxBinaryMessageSize here too. Check the standard web.xml for further informations.
  • 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 be closed. In this case, the ws:error annotation will be called.
  • Annotate your specific XQuery-Functions with WebSocketAnnotations. You have to specify at least one WebSocket function.


To write functions that act as WebSocket functions you have to use 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 ws:connect('/') will be executed if a client establishes a connection with the path '/'. By using annotations its 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.


Called directly after a successful WebSocket handshake. The path specifies the path which a client is connected to. You can specify here how to handle your users, f.e. save a name as a WebSocket attribute. Furthermore, you can check here some of the header-params for validity.


Called when a message arrives at the server. The path specifies the path which a client is connected to. The message is the message sent by the client. Could be a text-message or a binary-message. It should be ensured that the format of the message correct.


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

ws:error(path, message)

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


For accessing specific parameters like the Http-Version or the Sec-WebSocket-Version.


The following list shows the parameters of a WebSocket. You can access the parameters via the annotation %ws:header-param(name,variable[,default])

  • 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


  • For interacting with other clients or manage specific clients you should check out the WebSocket Module as well.
  • The results of functions annotated with %ws:close or %ws:error will not be transmitted to the client
  • For keeping the connection alive it is possible to implement heart-beats
  • Use wss instead of ws for a secure WebSocket connection
  • If you use a proxy server, check its configuration if WebSocket support is enabled


The following chapter explains how to create a simple basic webapplication with websockets. You can find another example in the BaseX source code. First of all you have to ensure that the WsServlet is enabled in your web.xml. It will be enabled if you use the standard web.xml.

For establishing a connection to the WebSocket server it is necessary that the server provides at least one function annotated with a WebSocket annotation. Lets start by using the annotation %ws:connect('/'). In the connect function, specific WebSocket attributes like the id and name, emit a message to other connected users, write database entries, do nothing, ... can be set.

function example:connect() as empty-sequence() {

With the state until now you can create a connection between client and server. For doing sth. senseful with the WebSocket connection you should implement a function annotated with %ws:message("/").

import module namespace ws = 'http://basex.org/modules/ws'
  %ws:message('/', '{$message}')
function example:message(
  $message  as xs:string
) as empty-sequence() {

In the function above the WebSocketModule function emit is used for forwarding the message to all connected clients. Notice that you have to import the WebSocket Module before using it.

It is possible now to write client functions which connect to a WebSocket, send messages to the WebSocket and receive messages from the WebSocket. The following client example provides basic code for handling the WebSocket connection.

var ws = new WebSocket("wss://localhost:8984/ws");
ws.onmessage = function(event) {

function send(message) {

There are no heart-beats in this example. This means that the connection is terminated if nothing happens for 5 minutes (standard timeout). If you send a message which exceeds the textsize of 3kb defined in the web.xml the connection gets closed too.