• This repository has been archived on 30/Oct/2019
  • Stars
    star
    383
  • Rank 111,995 (Top 3 %)
  • Language
    Java
  • Created over 12 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swagger Socket: A REST over WebSocket

SwaggerSocket: A REST over WebSocket Protocol

Build Status

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.

Download SwaggerSocket

Using Maven or SBT

    <!-- Server side -->
    <dependency>
      <groupId>com.wordnik</groupId>
      <artifactId>swaggersocket-server</artifactId>
      <version>2.1.0</version>
    </dependency>

    <!-- Client side when using jquery.swaggersocket.js -->
    <dependency>
      <groupId>org.atmosphere.client</groupId>
      <artifactId>jquery</artifactId>
      <version>2.2.13</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>com.wordnik</groupId>
      <artifactId>swaggersocket.jquery</artifactId>
      <version>2.1.0</version>
      <type>war</type>
    </dependency>

    <!-- Client side when using swaggersocket.js -->
    <dependency>
      <groupId>org.atmosphere.client</groupId>
      <artifactId>javascript</artifactId>
      <version>2.2.13</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>com.wordnik</groupId>
      <artifactId>swaggersocket.js</artifactId>
      <version>2.1.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. By default, jetty9 is used in this case, but this can be changed with profile -Pjetty8

  % git clone https://github.com/swagger-api/swagger-socket.git
  % cd swagger-socket
  % mvn 
  % cd samples/swaggersocket-{sample_name}
  % mvn jetty:run

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

For Webapp samples, you can also download our war files and deploy them to any WebServer supporting WebSockets.

Note that both both Wordnik and Twitter Samples require a valid key to be configured in their corresponding web.xml file or passed as the command arguments when using nettosphere.sh. For details, refer to the README file of each sample project.

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>

In addition, configure additional init-param parameters required to run the application. For example, when using jersey to load resources under package com.wordnik.swaggersocket.samples.

    <servlet>
        ...
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.wordnik.swaggersocket.samples</param-value>
        </init-param>

SwaggerSocket JavaScript API

The SwaggerSocket Client is defined as

    // use new jQuery.swaggersocket... when using jQuery, otherwise use new swaggersocket...
    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().

    // use new jQuery.swaggersocket... when using jQuery, otherwise use new swaggersocket...
    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

    // use new jQuery.swaggersocket... when using jQuery, otherwise use new swaggersocket...
    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();
    // use new jQuery.swaggersocket... when using jQuery, otherwise use new swaggersocket...
    requests[0] = new jQuery.swaggersocket.Request()
            .path("path1")
            .method("POST")
            .data("FOO")
            .dataFormat("text/plain")
            .listener(ss);
    requests[1] = new jQuery.swaggersocket.Request()
            .path("/path2")
            .method("POST")
            .data("BAR")
            .dataFormat("text/plain")
            .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!")
      .dataFormat("application/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

SwaggerSocket on Node.js

SwaggerSocket client (From 2.0.0) is available for Node.js. To see how it works, see the swaggersocket-echo-node-client sample.

SwaggerSocket on OSGi

SwaggerSocket (From 2.0.1) is not only OSGi enabled but also available as a Karaf feature. To see how it works, see the swaggersocket-cxf-osgi-echo sample.

How the protocol works

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

Versions

2.1.x release: 2.1.0

2.0.x release: 2.0.2 2.0.1 2.0.0


More Repositories

1

swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
JavaScript
26,260
star
2

swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
Mustache
16,866
star
3

swagger-editor

Swagger Editor
JavaScript
8,842
star
4

swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
Java
7,372
star
5

swagger-node

Swagger module for node.js
JavaScript
3,969
star
6

swagger-js

Javascript library to connect to swagger-enabled APIs via browser or nodejs
JavaScript
2,607
star
7

swagger.io-docs

The content of swagger.io
Astro
1,560
star
8

swagger-parser

Swagger Spec to Java POJOs
Java
777
star
9

swagger-samples

Samples for the various Swagger projects under swagger-api
JavaScript
537
star
10

swagger-play

Java
330
star
11

swagger-codegen-generators

Mustache
284
star
12

swagger-petstore

Java
245
star
13

validator-badge

Validate your Swagger JSON/YAML today!
Java
210
star
14

swagger-inflector

Java
159
star
15

swagger-converter

OpenAPI/Swagger 2.0 to OpenAPI 3.0 Converter WebService
Shell
114
star
16

swagger-scala-module

Swagger support for scala
Scala
103
star
17

apidom

Semantic parser for API specifications
TypeScript
68
star
18

rails-petstore

Ruby
29
star
19

swaggerhub-maven-plugin

A simple maven plugin to access SwaggerHub hosting of OpenAPI/Swagger from a maven build process.
Java
29
star
20

swagger-play-sample-app

A sample play app which uses swagger plugin to make the age old pet store swagger compliant.
JavaScript
29
star
21

swagger2

For working out the Swagger 2 working group page
CSS
21
star
22

scalatra-sample-app

Shell
20
star
23

swagger-scala-sample-app

A fully-functioning, stand-alone Swagger server written in scala which demonstrates how to enable Swagger in your API.
Scala
20
star
24

swagger-form-editor

JavaScript
19
star
25

swaggerhub-gradle-plugin

Gradle plugin for SwaggerHub
Java
19
star
26

petstore-kafka

A demo site built on top of Kafka topics
JavaScript
13
star
27

swagger-editor-cra

This is forked Create React App that builds SwaggerEditor@5
JavaScript
10
star
28

swagger-async-httpclient

Scala
10
star
29

swagger-play-1.2

Scala
9
star
30

swagger-scala

Scala
8
star
31

swagger-schema-packaging

6
star
32

apidom-lsp-vscode

ApiDOM VS Code Extension
TypeScript
5
star
33

sway-worker

4
star
34

apidom-ls-client

Demo client for apidom-ls OpenAPI / AsyncAPI ApiDOM validation service
TypeScript
3
star
35

.github

Common Github files for the Swagger projects
3
star
36

swagger-codegen-test

1
star