gwestersf / swaggersocket

Swagger Socket: A REST over WebSocket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwaggerSocket: A REST over WebSocket Protocol

The SwaggerSocket protocol allows any existing REST Resources to be executed on top of the WebSocket Protocol. Resources can be deployed as it is, without any modification and take advantage of the SwaggerSocket protocol.

Join the community

You can subscribe to our Google Group or follow us on Twitter

Download SwaggerSocket

Using Maven or SBT

    <!-- Server side -->
    <dependency>
       <groupId>com.wordnik</groupId>
       <artifactId>swaggersocket</artifactId>
       <version>1.0.0</version>
    </dependency>
    <!-- Client side --> 
     <dependency>
       <groupId>com.wordnik</groupId>
       <artifactId>swaggersocket.js</artifactId>
       <version>1.0.0</version>
       <type>war</type>
    </dependency>   

Manual download here

Getting Started

The quickest way to see how the protocol works is to try the samples. You can download them from here. Just do

  % unzip swaggersocket-{sample_name}-distribution.zip
  % chmod a+x ./bin/nettosphere.sh
  % ./bin/nettosphere.sh

and then point your browser to http://127.0.0.1:8080

You can also build the sample yourself and use Jetty instead of NettoSphere.

  % git clone git@github.com:wordnik/swagger-sockets.git
  % cd swagger-sockets
  % mvn 
  % cd samples/swaggersocket-echo OR samples/swaggersocket-twitter
  % mvn jetty:run

Take a look at HelloWorld mini tutorial. You can also look at our real time samples:

You can also download our war files and deploy them to any WebServer supporting WebSockets.

Add bi-directional support to your REST application

You can also add bi-directional support to your REST resources by extending your application using the Atmosphere Framework.

Quick Overview

To enable SwaggerSocket, add the following in your web.xml.

    <servlet>
        <description>SwaggerSocketServlet</description>
        <servlet-name>SwaggerSocketServlet</servlet-name>
        <servlet-class>com.wordnik.swaggersocket.server.SwaggerSocketServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

SwaggerSocket JavaScript API

The SwaggerSocket Client is defined as

    var swaggerSocket = new jQuery.swaggersocket.SwaggerSocket();

Listeners are per Request. The responses will be delivered as they come and can be correlated with their associated request using response.getRequest().

    var ss = new jQuery.swaggersocket.SwaggerSocketListener();
    ss.onOpen = function(response) {};
    ss.onClose = function(Response) {}; // Called when the Websocket gets closed
    ss.onError = function(Response) {}; // When an error occurs
    ss.onResponse = function(Response) {}; // When a response is ready
    ss.onResponses = function (Response) {}; // A List of all the ready responses

Opening a connection

     var request = new jQuery.swaggersocket.Request()
           .path(document.location.toString())
           .listener(ss);
     swaggerSocket.open(request);

Sending requests -- You can send an array of Requests or single Request.

    var requests = new Array();
    requests[0] = new jQuery.swaggersocket.Request()
            .path("path1")
            .method("POST")
            .data("FOO")
            .listener(ss);
    requests[1] = new jQuery.swaggersocket.Request()
            .path("/path2")
            .method("POST")
            .data("BAR")
            .listener(ss);
    swaggerSocket.send(requests);

SwaggerSocket Scala API

The SwaggerSocket protocol can also be used using the Scala language. The SwaggerSocket Scala library is using Asynchronous I/O so all the API calls are non blocking. First, you need to hanshake using

    val ss = SwaggerSocket().open(new Request.Builder().path(getTargetUrl + "/").build())

Then you are ready to start sending requests. As simple as:

    // send (Request, SwaggerSoketListener )
    // or send (Array[Request], SwaggerSoketListener)
    ss.send(new Request.Builder()
      .path("/b")
      .method("POST")
      .body("Yo!")
      .format("JSON")
      .build(), new SwaggerSocketListener() {
      	override def error(e: SwaggerSocketException) {
           // Invoked when an exception occurs
	    }
     	override def message(r: Request, s: Response) {
           // Response is delievered here
        }
      })

Once completed, you just need to close

    ss.close

How the protocol works

To read more about how the protocol works, take a look at the SwaggerSocket Protocol Specification

About

Swagger Socket: A REST over WebSocket