Yes Christian,
it works with ws:emit() and I also understand the point now.
This embedded support for websockets allows us to reduce our code base in some use cases by several hundreds lines of code. Not speaking about dependency management and configuration tricks to be documented.
Great job!
M.

On 22/10/18 13:36, Christian Grün wrote:
Hi Marco,

I’m glad to you are gathering some experience with the WebSocket facility.

I get the following error [2] as return to my HTTP POST and, ca va sans
dire, nothing on the websocket.
The function bound to the "/dataprovider" is a simple RESTXQ function.
As such, it isn’t attached to a WebSocket id. One of the reasons is
that a client who’s using RESTXQ may not necessarily have a WebSocket
connection, or there can also be multiple WebSockets per client.

If you want to send your result to all WebSockets – including the
client that called the dataprovider – you can simply use ws:emit(). If
your use case is complex enough to have a WebSocket connection and
simultaneous RESTXQ requests in a single browser tab, you could store
the WebSocket id(s) of your client as HTTP Session attribute, and
access these ids from the RESTXQ code.

BTW, even if not stated in the path annotation, an extra /ws needs to be
prefixed to the url used for JS' WebSocket constructor. Personally I'd
prefer to keep things explicit and put it in the annotation too.
I have just revised our documentation, and I hope it’s fairly complete
now. In the Annotations Section, you will find a hint to the "ws/"
path.

The reason why the path is omitted in XQuery is that the web server
takes care of the path resolution. If the default path is changed in
the web.xml file, it would need to be changed in all XQuery
applications as well. The same applies to RESTXQ: If a prefix is used
in the configuration, there won’t be a need to change your path
annotations.

Hope this helps,
Christian

[1] http://docs.basex.org/wiki/WebSockets


Thanks for your support and thanks Maximilian for the lift.

[1]
module namespace dp = 'urn:nubisware:datarouter';

import module namespace ws = 'http://basex.org/modules/ws';

declare
    %rest:path('/dataprovider')
    %rest:POST("{$data}")
    %output:method('json')
function dp:route($data as node()) {
    ws:send(json:serialize($data), ws:ids()[. != ws:id()])
};

declare
    %ws:connect('/dataprovider')
function dp:connect() as empty-sequence() {
    ()
};

declare
    %ws:close('/dataprovider')
function dp:close() as empty-sequence() {
    ()
};

[2]
Stopped at /home/lettere/tmp/basex/webapp/dataprovider/dataprovider.xqm,
11/53:
[basex:ws] WebSocket connection required.

On 18/10/18 18:33, Christian Grün wrote:
Sir, doing our best ;)

We believe that BaseX 9.1 pretty soon (until end of October).

For everyone who is interested in giving us some feedback on the new
WebSocket feature… Thank you in advance! 90% of the documentation is
finalized:

    http://docs.basex.org/wiki/WebSockets

Best,
Christian


Reply via email to