SwaggerSocket Documentation

repository·master·Indexed 18 days ago

https://github.com/swagger-api/swagger-socket

SwaggerSocket is a protocol that enables REST resources to be executed over WebSockets, allowing for bi-directional communication while maintaining RESTful semantics. It provides server-side implementation via SwaggerSocketServlet and client-side APIs for JavaScript (including a jQuery-based version) and Scala. The library supports integration with Maven, Jetty, NettoSphere, and Apache Karaf OSGi containers.

Tokens
4.5K
Snippets
12
Records
19
Agent score
64%

What's inside SwaggerSocket

  1. Run a SwaggerSocket sample using Maven Jetty

    master

    You can run a SwaggerSocket sample locally using the Maven Jetty plugin. By default, Jetty runs on port 8080. To specify a different port, use the jetty.port system property.

    # Standard run on port 8080
    % cd swaggersocket-{name}
    % mvn jetty:run
    
    # Run on a custom port (e.g., 9090)
    % mvn -Djetty.port=9090 jetty:run
  2. Run SwaggerSocket samples

    master

    You can quickly explore the protocol by running pre-built distributions.

    1. Unzip the distribution file.
    2. Make the nettosphere.sh script executable.
    3. Run the script.
    4. Access the interface at http://127.0.0.1:8080.

    Alternatively, you can build from source using Maven and run with Jetty (defaulting to Jetty 9, or use -Pjetty8 for Jetty 8):

    % git clone https://github.com/swagger-api/swagger-socket.git
    % cd swagger-socket
    % mvn 
    % cd samples/swaggersocket-{sample_name}
    % mvn jetty:run
    % unzip swaggersocket-{sample_name}-distribution.zip
      % chmod a+x ./bin/nettosphere.sh
      % ./bin/nettosphere.sh
  3. Set up the SwaggerSocket CXF Echo Demo on Karaf

    master

    This guide demonstrates how to run an echo demo using SwaggerSocket with Apache CXF within an Apache Karaf OSGi container.

    1. Prepare Apache Karaf

    Download and unpack Apache Karaf 3.0.4:

    $ wget -N http://ftp.halifax.rwth-aachen.de/apache/karaf/3.0.4/apache-karaf-3.0.4.tar.gz
    $ tar -zxf apache-karaf-3.0.4.tar.gz
    $ cd apache-karaf-3.0.4
    $ bin/karaf

    2. Install SwaggerSocket Server

    Inside the Karaf console, add the SwaggerSocket feature repository and install the server component:

    karaf@root()> feature:repo-add mvn:io.swagger/swaggersocket-karaf-features/2.2.0-SNAPSHOT/xml/features
    karaf@root()> feature:install swaggersocket-server

    3. Install CXF and Web Support

    Install the necessary CXF JAX-RS components and Karaf's WAR support:

    karaf@root()> feature:repo-add cxf 3.0.7
    karaf@root()> feature:install cxf-jaxrs
    karaf@root()> feature:install war

    4. Install the Echo Sample Bundle

    Install the specific SwaggerSocket CXF echo sample bundle:

    karaf@root()> install -s mvn:io.swagger/swaggersocket-cxf-sample-osgi-echo/2.2.0-SNAPSHOT

    5. Verify Installation

    Check that the bundle is Active using list and that the web application is Deployed using web:list:

    karaf@root()> list
    karaf@root()> web:list

    6. Access the Demo

    Open the following URL in your browser: http://localhost:8181/swaggersocket-cxf-echo/

  4. Configure Twitter Consumer credentials for the demo

    master

    To run the Twitter Search demo, you must provide a valid Twitter Consumer Key and Secret. You can obtain these by creating an application at http://apps.twitter.com/.

    Depending on your deployment method, configure the credentials as follows:

    Web Application

    If deploying as a Web Application, add the following init-param entries to your web.xml file:

    nettosphere.sh

    If using nettosphere.sh, pass the key and secret as command line arguments. Alternatively, running the script without arguments will prompt you to enter them manually (available in version 2.0.1+).

    <!-- Web Application: web.xml configuration -->
    <init-param>
        <param-name>com.twitter.consumer.key</param-name>
        <param-value>${your-consumer-key}</param-value>
    </init-param>
    <init-param>
        <param-name>com.twitter.consumer.secret</param-name>
        <param-value>${your-consumer-secret}</param-value>
    </init-param>
    
    <!-- nettosphere.sh: Command line arguments -->
    ./bin/nettosphere.sh ${your-consumer-key} ${your-consumer-secret}
  5. Configure the Wordnik API key for the SwaggerSocket demo

    master

    To run the Wordnik Search Demo, you must provide a valid Wordnik API key. You can obtain one from http://developer.wordnik.com.

    Depending on your deployment method, configure the key as follows:

    Web Application

    Add the following init-param to your web.xml file:

    Nettosphere Shell

    Pass the API key as a command-line argument when running the script, or run it without arguments to be prompted for the key (available in version 2.0.1+).

    <!-- Web Application: web.xml configuration -->
    <init-param>
        <param-name>com.wordnik.swagger.key</param-name>
        <param-value>${your-api-key}</param-value>
    </init-param>
    
    # Nettosphere Shell: command line usage
    ./bin/nettosphere.sh ${your-api-key}
  6. Run a SwaggerSocket sample using NettoSphere

    master

    You can run a sample using the NettoSphere embedded server by extracting the distribution zip file. The default port is 8080. To change the port, set the nettosphere.port system property via the DEV_OPTS environment variable.

    # Standard run
    % cd swaggersocket-{name}/target
    % unzip swaggersocket-{name}-distribution.zip
    % chmod a+x bin/nettosphere.sh
    % ./bin/nettosphere.sh
    
    # Run on a custom port (e.g., 9090) using DEV_OPTS
    % export DEV_OPTS="-Dnettosphere.port=9090"
    % ./bin/nettosphere.sh
  7. Install SwaggerSocket via Maven

    master

    To use SwaggerSocket in your project, add the following dependencies to your pom.xml. Note that client-side dependencies vary depending on whether you are using the jQuery-based client or the standard JavaScript client.

    <!-- 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>
  8. Run the Node.js Echo Demo client

    master

    This demo uses swaggerSocket.js to create a client that echoes messages back from a SwaggerSocket server.

    Prerequisites:

    1. A running SwaggerSocket server. You can use the swaggersocket-echo sample server found in the samples/swaggersocket-echo directory of this repository.
    2. Node.js installed on your system.

    Steps to run:

    1. Navigate to the client source directory: src/main/resources.
    2. Install the required client dependency: swaggersocket-client.
    3. Execute the client script: echo-client.js.
    % cd src/main/resources
    % npm install swaggersocket-client
    % node echo-client.js
  9. Configure SwaggerSocketServlet in web.xml

    master

    To enable SwaggerSocket on your server, register the SwaggerSocketServlet in your web.xml. If you are using a framework like Jersey to load resources, you must also configure the package scanning via init-param.

    <!-- Register the Servlet -->
    <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>
    
    <!-- Example: Configure Jersey package scanning -->
    <servlet>
        ...
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.wordnik.swaggersocket.samples</param-value>
        </init-param>
    </servlet>