question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket support

See original GitHub issue

Have you ever thought about adding WebSocket support?

I am looking for a way to push XML messages to clients to enable multi-client xml-applications and WebSockets would be an ideal fit. I added a %rest:WEBSOCKET annotation to test the concept and got it to work, so I was wondering if something like this was ever on the roadmap for BaseX/RESTXQ?

Thanks! Mike

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:33 (28 by maintainers)

github_iconTop GitHub Comments

3reactions
jfinckhcommented, Jul 27, 2018

The first implementation of websockets can be found in the masterbranch now.

Import in XQuery:

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

Overview of the new Annotations:

  • ws:connect(path) -> Called when a Websocket connects
  • ws:message(path,message) -> Called when a Message arrives
  • ws:close(path) -> Called when a Websocket closes
  • ws:error:(path,message) -> Called when a Websocket throws an error
  • ws:param(name,variable[,default]) -> For getting additional Parameters

Possible ws:param’s:

  • 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

Overview of new Functions

  • ws:emit(message) -> Emits a Message to all Connected Clients
  • ws:broadcast(message) -> Broadcasts a Message to all Connected Clients except the Caller
  • ws:send(message,id) -> Sends a Message to the Client with the id id
  • ws:id() -> Returns the Id of the Caller
  • ws:ids() -> Returns the IDs of all connected Clients
  • ws:get(key) -> Returns a WebsocketAttribute of the calling Client
  • ws:get(id,key) -> Returns a WebsocketAttribute of the Client with the id id
  • ws:set(key,value) -> Sets a WebsocketAttribute of the calling Client
  • ws:set(id, key, value) -> Sets a WebsocketAttribute of the Client with the id id
  • ws:delete(key) -> Deletes a WebsocketAttribute of the calling Client
  • ws:delete(id, key) -> Deletes a WebsocketAttribute of the Client with the id id
  • ws:path() -> Returns the Path of the calling Client
  • ws:path(id) -> Returns the Path of the Client with the id id

The Path is always the Path the Client connects to.

A quick-and-dirty-example for the most of the new Annotations/Functions can be found here: https://github.com/jfinckh/chat/tree/master

We would be pleased about feedback and further extensions.

3reactions
usqcommented, Apr 18, 2017

Hi, sorry for the late reply, I was swamped with work the last days.

Thanks for the mailing list link!

I just added https://github.com/TooTallNate/Java-WebSocket, added the %rest:WEBSOCKET annotation and send the serialized XML via the WebSocket to all connected clients when creating the RestXqResponse. But this was just for a prototype and is far from production code!

The websocket support is actually part of my master thesis on multi-client XML applications. I need to notify a group of connected clients about changes to the database (e.g. for an XML chat application, or multi-client word processing like Google Docs)

At the moment, I’m trying to figure out an API/annotation-parameter to specify that WebSocket messages should only be sent to either all clients, a subgroup or just one. An example would be a chat server, which allows users to join specific chat rooms. As soon as a message is posted and stored in the database, only the clients inside the room should receive a WebSocket message containing the new database state.

As soon as I got that, I’ll post a clean/non-hacky pull request (adding the WebSocket like mentioned on the mailing list).

Read more comments on GitHub >

github_iconTop Results From Across the Web

The WebSocket API (WebSockets) - Web APIs - MDN Web Docs
desktop desktop Chrome Edge WebSocket Full support. Chrome4. Toggle history Full support. Edge12. Toggl... WebSocket() constructor Full support. Chrome4. Toggle history Full support. Edge12. Toggl...
Read more >
Web Sockets | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
WebSocket - Wikipedia
WebSocket is a computer communications protocol, providing full-duplex communication ... Most browsers support the protocol, including Google Chrome, Firefox, ...
Read more >
25. WebSocket Support - Spring
WebSocket Support. This part of the reference documentation covers Spring Framework's support for WebSocket-style messaging in web applications including use of ...
Read more >
What is WebSocket and How It Works? ⚙️ - Wallarm
WebSocket enables two-way interactive communication sessions between client and server without having to poll the server for a reply, providing both speed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found