Bitquery_Websocket_Java
unknown
java
a year ago
6.1 kB
3
Indexable
Never
//BitqueryWebsocketClient.java package org.example; import okhttp3.*; import org.json.JSONException; import org.json.JSONObject; import java.util.concurrent.TimeUnit; public class BitqueryWebsocketClient { private static final String API_KEY = "*************************"; private static final String WS_URL = "wss://streaming.bitquery.io/graphql"; public static void main(String[] args) { OkHttpClient client = new OkHttpClient.Builder() .readTimeout(0, TimeUnit.MILLISECONDS) .build(); Request request = new Request.Builder() .url(WS_URL) .addHeader("Authorization", "Bearer " + API_KEY) .addHeader("Sec-WebSocket-Protocol", "graphql-ws") .build(); WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() { @Override public void onOpen(WebSocket webSocket, Response response) { System.out.println("Opened connection"); // Initialization message String initMessage = "{ \"type\": \"connection_init\" }"; webSocket.send(initMessage); // Wait for a bit (this is just an example, you might want a more structured approach) try { Thread.sleep(1000); // Sleep for 1 second } catch (InterruptedException e) { e.printStackTrace(); } // Send the updated subscription message String payload = new JSONObject() .put("id", "1") .put("type", "start") .put("payload", new JSONObject() .put("query", "subscription MyQueryWithVariables($network: evm_network!, $limit: Int!) {" + " EVM(network: $network){" + " Transfers(limit: {count: $limit}, orderBy: {descending: Block_Number}) {" + " Block {" + " Number" + " Hash" + " }" + " Transfer {" + " Currency {" + " Symbol" + " Name" + " }" + " Amount" + " }" + " }" + " }" + "}") .put("variables", new JSONObject() .put("network", "eth") .put("limit", 2))) .toString(); webSocket.send(payload); } @Override public void onMessage(WebSocket webSocket, String text) { try { JSONObject jsonObject = new JSONObject(text); if (!"ka".equals(jsonObject.optString("type"))) { System.out.println("Received: " + text); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onClosed(WebSocket webSocket, int code, String reason) { System.out.println("Closed: " + reason); } @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { t.printStackTrace(); } }); // To keep the program running try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); } } } /////////////////////////////////////////////////////////////////////////////////// //pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Bit</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- WebSocket Client --> <dependency> <groupId>org.java-websocket</groupId> <artifactId>Java-WebSocket</artifactId> <version>1.5.1</version> </dependency> <!-- JSON Handling --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20210307</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.30</version> <!-- Or the latest version available --> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.0</version> </dependency> <!-- Add any other dependencies you have here --> </dependencies> </project>