I am using bitfinex websockets v2 api in a Java application, to get the candles from a market. But I don't get any message back from the socket(not even an error). I wonder what I am doing wrong. Also I noticed that I can't subscribe to the ticker.
v1 of the api works fine for me I can subscribe to messages with no problem. But in v2, I only get the ping pong to work but I can't subscribe to messages such as ticker or candles.
Let me show you the code that I have, maybe I am forgetting something:
Here I create a client and open a websocket to the new api address:
@SpringBootApplication(scanBasePackages = "application")
public class Application {
private static Object waitLock = new Object();
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
WebSocketClient webSocketClient = new WebSocketClient();
webSocketClient.startWebSocketClient("wss://api.bitfinex.com/ws/2");
}
}
This is the client that manages the connection to the socket
public class WebSocketClient {
final static CountDownLatch messageLatch = new CountDownLatch(1);
public void startWebSocketClient(String address) {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
System.out.println("Connecting to " + address);
container.connectToServer(WebSocketEndpoint.class, URI.create(address));
messageLatch.await();
} catch (DeploymentException | InterruptedException | IOException ex) {
Logger.getLogger(WebSocketClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
This is the endpoint that I create to handle the responses
@ClientEndpoint
public class WebSocketEndpoint {
private Session session;
@OnOpen
public void onOpen(Session session) {
this.session = session;
System.out.println("Connected to endpoint: " + session.getBasicRemote());
RemoteEndpoint.Async asyncRemote = session.getAsyncRemote();
asyncRemote.sendText("{event:\"subscribe\",channel:\"candles\",key:\"trade:1m:tBTCUSD\"}");
}
@OnMessage
public void processMessage(String message) {
System.out.println("Received message: " + message);
}
@OnError
public void processError(Throwable t) {
t.printStackTrace();
}
}