Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocols for communicating over web sockets

I'm working on the client-side of a project with a large and complex server-side component. The client will be deployed as an mobile app among other contexts.

For client-server communication, there are two opposing views:

  • Use REST
  • Use web sockets

Personally, I don't mind which approach is taken so long as the resulting API is well thought out, understandable and extensible.

From experience with using TCP sockets before on a complex C++-based application, I know that roll-your-own syntax/protocols can quickly get inconsistent, confusing and difficult to manage.

Are there any general purpose styles or protocols, like REST or SOAP, for client-server communication using web sockets? Are there any guidelines or best practices on designing your own client-server communication scheme/protocol?

like image 480
Oliver Moran Avatar asked Jan 04 '13 19:01

Oliver Moran


People also ask

Which protocol is used by WebSockets?

The WebSocket protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. By default the WebSocket protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS [RFC2818].

How do WebSockets communicate?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.

Do WebSockets allow full-duplex communication?

WebSocket is a standard protocol that enables a web browser or client application, and a web server application to use a full-duplex connection to communicate.

What can be sent over WebSocket?

Sending Messages With WebSocket You can send both text and binary data through a WebSocket. For your demo application you need to send the contents of the textarea to the server when the form is submitted. To do this you first need to set up an event listener on the form.


2 Answers

Have you looked at WAMP?

From the above page:

The WebSocket Protocol is already built into modern browsers and provides bidirectional, low-latency message-based communication. However, as such, WebSocket it is quite low-level and only provides raw messaging.

Modern Web applications often have a need for higher level messaging patterns such as Publish & Subscribe and Remote Procedure Calls.

This is where The WebSocket Application Messaging Protocol (WAMP) enters. WAMP adds the higher level messaging patterns of RPC and PubSub to WebSocket - within one protocol.

Technically, WAMP is an officially registered WebSocket subprotocol (runs on top of WebSocket) that uses JSON as message serialization format.

WAMP embraces open Web standards and was designed to be easy to use and simple to implement.

like image 61
guettli Avatar answered Sep 21 '22 18:09

guettli


Without any intended slight, Jesse, I'm going to answer my own question after some research.

I didn't come across any equivalents to REST. The current trend appears to be to use JSON to send and receive objects. That seems sensible in a JavaScript-orientated world and allows messages to immediately update data on receipt.

For example:

  • https://developer.mozilla.org/en-US/docs/WebSockets/Writing_WebSocket_client_applications
  • http://blog.new-bamboo.co.uk/2010/2/10/json-event-based-convention-websockets

I gave a stab at writing my own protocol along these lines. However, the most fully defined protocol along I came across is JSON-RPC. An additional plus about that protocol is that the same messaging system can be used over HTTP and WebSockets, should you be writing in a mixed-sockets and HTTP application.

Another approach I came across was to "port" exiting messaging protocols to WebSockets (regardless of whether they use JSON or not). So, for example, XML-RPC (which JSON-RPC is based on) could be re-implemented fairly simply for use over sockets. Indeed, SOAP also could be re-implemented over sockets.

A nice, small, protocol I came across though one of the above links was STOMP. That could also be ported - and indeed has.

like image 39
Oliver Moran Avatar answered Sep 19 '22 18:09

Oliver Moran