Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket request-response subprotocol

Tags:

websocket

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?

like image 576
Ghetolay Avatar asked Jun 04 '12 13:06

Ghetolay


2 Answers

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.

like image 134
oberstet Avatar answered Oct 05 '22 22:10

oberstet


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…

like image 22
fiddur Avatar answered Oct 06 '22 00:10

fiddur