Run a SwaggerSocket sample using an external container
masterswaggersocket-{name}.war file. Ensure that your chosen web server supports the WebSocket Protocol.repository·master·Indexed 18 days ago
https://github.com/swagger-api/swagger-socketSwaggerSocket 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.
swaggersocket-{name}.war file. Ensure that your chosen web server supports the WebSocket Protocol.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:runYou can quickly explore the protocol by running pre-built distributions.
nettosphere.sh script executable.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.shThis guide demonstrates how to run an echo demo using SwaggerSocket with Apache CXF within an Apache Karaf OSGi container.
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/karafInside 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-serverInstall 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 warInstall the specific SwaggerSocket CXF echo sample bundle:
karaf@root()> install -s mvn:io.swagger/swaggersocket-cxf-sample-osgi-echo/2.2.0-SNAPSHOTCheck that the bundle is Active using list and that the web application is Deployed using web:list:
karaf@root()> list
karaf@root()> web:listOpen the following URL in your browser:
http://localhost:8181/swaggersocket-cxf-echo/
README.md file located in the samples root folder of the repository for specific execution instructions.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:
If deploying as a Web Application, add the following init-param entries to your web.xml file:
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}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:
Add the following init-param to your web.xml file:
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}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.shTo 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>This demo uses swaggerSocket.js to create a client that echoes messages back from a SwaggerSocket server.
Prerequisites:
swaggersocket-echo sample server found in the samples/swaggersocket-echo directory of this repository.Steps to run:
src/main/resources.swaggersocket-client.echo-client.js.% cd src/main/resources
% npm install swaggersocket-client
% node echo-client.jsREADME.md file located in the samples root folder of the repository for specific execution instructions.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>