WebSocket-Resources

A Dropwizard library that lets you use Jersey-style Resources over WebSockets

License

License

GroupId

GroupId

org.whispersystems
ArtifactId

ArtifactId

websocket-resources
Last Version

Last Version

0.5.10
Release Date

Release Date

Type

Type

jar
Description

Description

WebSocket-Resources
A Dropwizard library that lets you use Jersey-style Resources over WebSockets
Project URL

Project URL

https://github.com/WhisperSystems/WebSocket-Resources
Source Code Management

Source Code Management

https://github.com/WhisperSystems/WebSocket-Resources

Download websocket-resources

How to add to project

<!-- https://jarcasting.com/artifacts/org.whispersystems/websocket-resources/ -->
<dependency>
    <groupId>org.whispersystems</groupId>
    <artifactId>websocket-resources</artifactId>
    <version>0.5.10</version>
</dependency>
// https://jarcasting.com/artifacts/org.whispersystems/websocket-resources/
implementation 'org.whispersystems:websocket-resources:0.5.10'
// https://jarcasting.com/artifacts/org.whispersystems/websocket-resources/
implementation ("org.whispersystems:websocket-resources:0.5.10")
'org.whispersystems:websocket-resources:jar:0.5.10'
<dependency org="org.whispersystems" name="websocket-resources" rev="0.5.10">
  <artifact name="websocket-resources" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.whispersystems', module='websocket-resources', version='0.5.10')
)
libraryDependencies += "org.whispersystems" % "websocket-resources" % "0.5.10"
[org.whispersystems/websocket-resources "0.5.10"]

Dependencies

compile (7)

Group / Artifact Type Version
io.dropwizard : dropwizard-core jar 1.3.9
io.dropwizard : dropwizard-auth jar 1.3.9
io.dropwizard : dropwizard-client jar 1.3.9
io.dropwizard : dropwizard-servlets jar 1.3.9
org.eclipse.jetty.websocket : websocket-server jar 9.4.14.v20181114
com.google.protobuf : protobuf-java jar 2.6.1
io.dropwizard : dropwizard-testing jar 1.3.9

test (1)

Group / Artifact Type Version
org.mockito : mockito-core jar 2.7.22

Project Modules

There are no modules declared in this project.

WebSocket-Resources

A Dropwizard library that lets you use Jersey-style Resources over WebSockets.

Install from maven central:

<dependency>
  <groupId>org.whispersystems</groupId>
  <artifactId>websocket-resources</artifactId>
  <version>${latest_version}</version>
</dependency>

The problem

In the standard HTTP world, we might use Jersey to define a set of REST APIs:

@Path("/api/v1/mail")
public class MailResource {
  
  @Timed
  @POST
  @Path("/{destination}/")
  @Consumes(MediaType.APPLICATION_JSON_TYPE)
  public void sendMessage(@Auth Account sender, 
                          @PathParam("destination") String destination,
                          @Valid Message message) 
  {
    ...
  }
}

Using JAX-RS annotations and some Dropwizard glue, we can easily define a set of resource methods that allow an authenticated sender to POST a JSON Message object. All of the routing, parsing, validation, and authentication are taken care of, and the resource method can focus on the business logic.

What if we want to expose a similar API over a WebSocket? It's not pretty. We have to define our own sub-protocol, do all of the parsing and validation ourselves, keep track of the connection state, and do our own routing. It's basically the equivalent of writing a raw servlet, but worse.

The WebSocket-Resources model

WebSocket-Resources is designed to make exposing an API over a WebSocket as simple as writing a Jersey resource. The library is based on the premise that the WebSocket client and the WebSocket server should each be modeled as both a HTTP client and server simultaneously.

That is, the WebSocket server receives HTTP-style requests and issues HTTP-style responses, but it can also issue HTTP-style requests to the client, and expects HTTP-style responses from the client. This allows us to write Jersey-style resources, while also initiating bi-directional communication from the server.

What if we wanted to make the exact same resource above available over a WebSocket using WebSocket-Resources? In your standard Dropwizard service run method, just initialize WebSocket-Resources and register a standard Jersey resource:

  @Override
  public void run(WhisperServerConfiguration config, Environment environment)
      throws Exception
  {
    WebSocketEnvironment webSocketEnvironment = new WebSocketEnvironment(environment, config);
    webSocketEnvironment.jersey().register(new MailResource());
    webSocketEnvironment.setAuthenticator(new MyWebSocketAuthenticator());

    WebSocketResourceProviderFactory servlet   = new WebSocketResourceProviderFactory(webSocketEnvironment);
    ServletRegistration.Dynamic      websocket = environment.servlets().addServlet("WebSocket", servlet);

    websocket.addMapping("/api/v1/websocket/*");
    websocket.setAsyncSupported(true);
    servlet.start();
    
    ...    
  }

It's as simple as creating a WebSocketEnvironment from the Dropwizard Environment and registering Jersey resources.

Making requests

In order to call the Jersey resource we just registered from a client, we need to know how to format client requests. It's possible to either define our own subprotocol, or to use the default subprotocol packaged with WebSocket-Resources, which is based in protobuf.

A subprotocol is composed of Requests and Responses. A Request has four parts:

  1. An id.
  2. A method.
  3. A path.
  4. An optional body.

A Response has four parts:

  1. The request id it is in response to.
  2. A status code.
  3. A status message.
  4. An optional body.

This should seem strongly reminiscent of HTTP. By default, WebSocket-Resources will use a protobuf formatted subprotocol:

message WebSocketRequestMessage {
  optional string verb = 1;
  optional string path = 2;
  optional bytes  body = 3;
  optional uint64 id   = 4;
}

message WebSocketResponseMessage {
  optional uint64 id      = 1;
  optional uint32 status  = 2;
  optional string message = 3;
  optional bytes  body    = 4;
}

message WebSocketMessage {
  enum Type {
    UNKNOWN  = 0;
    REQUEST  = 1;
    RESPONSE = 2;
  }

  optional Type                     type     = 1;
  optional WebSocketRequestMessage  request  = 2;
  optional WebSocketResponseMessage response = 3;
}

To use a custom wire format, it's as simple as implementing a custom WebSocketMessageFactory and registering it at initialization time:

  @Override
  public void run(WhisperServerConfiguration config, Environment environment)
      throws Exception
  {
    WebSocketEnvironment webSocketEnvironment = new WebSocketEnvironment(environment);
    webSocketEnvironment.setMessageFactory(MyMessageFactory());
    ...
  }

Making requests from the server

To issue requests from the server, use WebSocketClient. There are two ways to get a WebSocketClient instance: a resource annotation or a connection listener.

Resource annotation:

@Path("/api/v1/mail")
public class MailResource {
  
  @Timed
  @POST
  @Path("/{destination}/")
  @Consumes(MediaType.APPLICATION_JSON_TYPE)
  public void sendMessage(@Auth Account sender, 
                          @WebSocketSession WebSocketSessionContext context, 
                          @PathParam("destination") String destination,
                          @Valid Message message) 
  {
    WebSocketClient client = context.getClient();
    ...
  }
}

Or a connect listener:

  @Override
  public void run(WhisperServerConfiguration config, Environment environment)
      throws Exception
  {
    WebSocketEnvironment webSocketEnvironment = new WebSocketEnvironment(environment);
    webSocketEnvironment.setConnectListener(new WebSocketConnectListener() {
      @Override
      public void onConnect(WebSocketSessionContext context) {
        WebSocketClient client = context.getClient();
        ...
      }
    });
    ...
  }

A WebSocketClient can then be issued to transmit requests:

  WebSocketClient client = context.getClient();
  
  ListenableFuture<WebSocketResponseMessage> response = client.sendRequest("PUT", "/api/v1/message", body);
  
  Futures.addCallback(response, new FutureCallback<WebSocketResponseMessage>() {
    @Override
    public void onSuccess(@Nullable WebSocketResponseMessage response) {
      ...
    }

    @Override
    public void onFailure(@Nonnull Throwable throwable) {
      ...
    }
  });

License

Copyright 2014 Open Whisper Systems

Licensed under the AGPLv3: https://www.gnu.org/licenses/agpl-3.0.html

org.whispersystems

Open Whisper Systems

The Signal repositories have moved to github.com/signalapp

Versions

Version
0.5.10
0.5.9
0.5.8
0.5.7
0.5.6
0.5.4
0.5.3
0.5.2
0.5.0
0.4.1
0.4.0
0.3.3
0.3.2
0.2.3
0.2.2
0.2.1
0.2.0
0.1