Ghetolay
Ghetolay

Reputation: 3342

WebSocket request-response subprotocol

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?

Upvotes: 96

Views: 65093

Answers (7)

oberstet
oberstet

Reputation: 22011

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.

Upvotes: 59

Roy
Roy

Reputation: 105

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.

Upvotes: 0

Neo
Neo

Reputation: 1392

(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)
});

Upvotes: 5

jfarcand
jfarcand

Reputation: 1705

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

Upvotes: 2

Andrew
Andrew

Reputation: 5340

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 .

Upvotes: 2

fiddur
fiddur

Reputation: 1857

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…

Upvotes: 15

Roger F. Gay
Roger F. Gay

Reputation: 1961

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

Upvotes: 1

Related Questions