WebSocket request-response sub-protocol

WebSocket provides bidirectional communication as if a person were talking. The client can send data to the server, and the server can send data to the client at any time. But what about request-response behavior? The client may ask the server something and wait for an answer. Websocket does not seem to provide anything to associate client data (request) with server data (response).

This is probably the work of the sub-protocol, and I have some ideas on how to do this (send an identifier with a request and wait for a response with the same identifier during the wait period).

In order not to reinvent the wheel and save time, I searched the Internet for an existing solution, but did not find anything related (possibly with bad keywords).

So, does anyone know about this work or am I missing something?

+69
websocket
Jun 04 2018-12-12T00:
source share
7 answers

The WebSocket messaging protocol (WAMP) http://wamp.ws/ provides RPC (remote procedure call) and PubSub (publish and sign) messaging templates on top of the original WebSocket for this purpose.

WAMP is the proper WebSocket subprotode, uses WebSocket as a transport, and JSON as a payload format. RPC is implemented using 3 messages, and these messages contain a β€œCall ID” to correlate the responses of the asynchronous RPC server to the calls of procedures initiated by the client.

Disclaimer: I am the author of WAMP and some (open source) WAMP implementations. His is an open initiative, while others have already begun to board the boat. Ultimately, there should be a WAMP RFC that correctly defines the protocol, but it is still in its early stages.

+46
Jun 04 2018-12-12T00:
source share

I would use JSON-RPC 2.0.

http://www.jsonrpc.org/specification

Each message will be a JSON object. The protocol indicates whether it is a call that wants to receive a response (communication with an identifier) ​​or a notification.

An application that supports JSON-RPC can easily check if the message object contains a method indicating the call or not, which means the response.

I am going to create a javascript lib to handle json rpc via websocket, with ajax as the backup ...

+12
Feb 13 '13 at 15:16
source share

take a look at SwaggerSocket , which is a REST protocol on top of WebSockets, supported by all major Java WebServer.

+2
Jul 19 '12 at 18:37
source share

I am running a simple request-response program using websockets. See " Websocket Server Demo ". You can download the source code of the web page.

+1
Jul 19 2018-12-17T00:
source share

Take a look at msg-rpc , it provides bidirectional rpc support through a simple messaging interface, including WebSocket.

Not only is it a simple rpc that can span the client / server request behavior, it also supports the server / client request behavior that runs through the Rpc service.

To get started, there are examples of sockjs and socket.io.

+1
Oct 22 '13 at 5:18
source share

(send an identifier with a request and wait for a response with the same identifier before the timeout)

I created a library that does just that, called WebSocketR2 (where R2 stands for response request): https://github.com/ModernEdgeSoftware/WebSocketR2

It also handles reconnecting to the server in the event of a loss of connection, which may be useful if you are running web sockets through a load balancer.

Ultimately, you can implement callbacks for the send function through a web socket as follows:

var request = { action: "login", params: { username: "test", password: "password" } }; ws.send(request, function(response){ console.log(response) }); 
+1
Jan 28 '18 at 14:41
source share

A bit late with this discussion, but BrokerJS is a reactive alternative that you can try in NodeJS. Define a data model and sign web socket connections to specific model keys. Any changes to the variable on the server side are automatically reflected on the client side. I think this will save you a lot of standard code. Moreover, you can still use old-fashioned web-based messaging in parallel with the new reactive mode of action. This is far from a polished product, and arrays are a headache. But when combined with something like VueJS, React or Svelte, I think this will save you a lot of problems.

Disclaimer: I am the author of BrokerJS.

0
Aug 12 '19 at 6:39 on
source share



All Articles