Java WebSocket

Publish with Java

How to subscribe? see here

Use the following code snippet to publish a message/event on a WebSocket Channel Room with Java.

public class Publish {

  public static void main(String[] args) {

    OkHttpClient client = new OkHttpClient().newBuilder().build();
    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(
      mediaType,
      //Demo secret and key, get yours at piesocket.com
      "{n    "key": "oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm",n    "secret": "d8129f82f8dd71910aa4a7efa30a7297",n    "roomId": 1,n    "message": "Hello world!"n}"
    );
    Request request = new Request.Builder()
      .url("https://CLUSTER_ID.piesocket.com/api/publish")
      .method("POST", body)
      .addHeader("Content-Type", "application/json")
      .build();
    Response response = client.newCall(request).execute();

  }

}

This sends a string message to Room 1.

To send an Event, use following message payload

{"event":"new-message","data":"Hello"}

Please note that this example uses a demo key and secret, which is public. The demo key is only for quick testing purposes, and you should not use this in your application.

You can get your free API key and API secret here.