WebSocket request-response subprotocol

Websocket

Websocket Problem Overview


WebSocket provides a bi-directional communication like a human being talks. The client can send data to the server and the server can send data to the client anytime. But what about the request-response behavior? The client could ask something to the server and wait for a response. It seems that Websocket doesn't provide anything to link client data (request) to server data (response).

It's probably the work of the subprotocol and I have some ideas on how to do it (send an ID with the request and wait for a reponse with the same ID within a timeout period).

In order to not reinvent the wheel and to save some time, I looked over the internet for an existing solution but I haven't found anything related (maybe bad keywords).

So, is there anyone aware about this kind of work or am I missing something?

Websocket Solutions


Solution 1 - Websocket

The WebSocket Application Messaging Protocol (WAMP) https://wamp-proto.org/ provides RPC (Remote Procedure Call) and PubSub (Publish & Subscribe) messaging patterns on top of raw WebSocket for that purpose.

WAMP is a proper WebSocket subprotocol, uses WebSocket as transport and JSON as a payload format. RPC is implemented using 3 messages, and those messages contain a "Call ID" to correlate asynchronous RPC server responses to client initiated procedure calls.

Disclaimer: I am author of WAMP and some (open-source) WAMP implementations. Its an open initiative, with others already started to get on the boat. Ultimately, there should be a WAMP RFC properly defining the protocol .. but its still in the early stages.

Solution 2 - Websocket

I would use JSON-RPC 2.0.

http://www.jsonrpc.org/specification

Each message would be a JSON object. The protocol states if it is a call that wants a response (coupling with id), or a notification.

A JSON-RPC aware application could easily check if the message object contains a method, signifying a call, or not, signifying a response.

I'm about to build a javascript lib to handle json rpc over websocket, with ajax as fallback…

Solution 3 - Websocket

> (send a id with the request and wait for a reponse with the same id until a timeout period)

I created a lib that does exactly that, called WebSocketR2 (where R2 means Request Response): https://github.com/ModernEdgeSoftware/WebSocketR2

It also handles reconnecting to the server if a connection is lost, which could be helpful if you are doing web sockets through a load balancer.

The final result is you can implement callbacks on the web socket send function like this:

var request = {
    action: "login",
    params: {
        username: "test",
        password: "password"
    }
};

ws.send(request, function(response){
    console.log(response)
});

Solution 4 - Websocket

take a look at the SwaggerSocket, which is a REST over WebSockets protocol supported with all major Java WebServer.

Solution 5 - Websocket

Take a look at msg-rpc, it provides bidirectional rpc support over simple message interface, include WebSocket.

Not only the simple rpc, which could cover "client request / server response" behavior, it also supports "server request / client response" behavior, which is via the Rpc Service.

To get start with, there're sockjs and socket.io examples .

Solution 6 - Websocket

I'm running a simple request-response program using websockets. See "Websocket Server Demonstration". You can download the webpage source code.

Solution 7 - Websocket

A bit late on this discussion but, BrokerJS is a reactive alternative you can try in NodeJS. Define a data model and subscribe websocket connections to specific keys of the model. Any changes to server side variable are automatically reflected on the client side. I think this will save you a lot of boilerplate code. Even better, you can still use the old-fashioned websocket messaging parallel to the new reactive way of doing things. It is far from a polished product and arrays are a headache. But in conjunction with something like VueJS, React or Svelte, I think it will save you a lot of trouble.

Disclaimer: I am the the author of BrokerJS.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGhetolayView Question on Stackoverflow
Solution 1 - WebsocketoberstetView Answer on Stackoverflow
Solution 2 - WebsocketfiddurView Answer on Stackoverflow
Solution 3 - WebsocketNeoView Answer on Stackoverflow
Solution 4 - WebsocketjfarcandView Answer on Stackoverflow
Solution 5 - WebsocketAndrewView Answer on Stackoverflow
Solution 6 - WebsocketRoger F. GayView Answer on Stackoverflow
Solution 7 - WebsocketRoyView Answer on Stackoverflow