<p>The problem might be, that the JSON data does not contain line breaks. I was facing the same issue. My 'subscribe' call had no effect until I added line breaks to the JSON data. I haven't even gotten an error message back from the API.</p>

<p>You could use JSON library for Java which will handle the correct formatting for you.</p>

<p>Please note, there are some existing Libraries for Bitfinex and Java. You don't need to implement the API calls on your own. </p>

<ul>
<li><a href="https://github.com/timmolter/XChange" rel="nofollow noreferrer">https://github.com/timmolter/XChange</a> (this one uses the REST API)</li>
<li><a href="https://github.com/jnidzwetzki/bitfinex-v2-wss-api-java" rel="nofollow noreferrer">https://github.com/jnidzwetzki/bitfinex-v2-wss-api-java</a> (this one uses the Websocket API)</li>
</ul>

CC BY-SA 3.0
2018-03-21T06:36:17.787Z
2018-03-21T06:36:17.787Z
Jens
<p>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.</p>

<p>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.</p>

<p>Let me show you the code that I have, maybe I am forgetting something:</p>

<p><em>Here I create a client and open a websocket to the new api address:</em></p>

<pre><code>@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");
    }
}
</code></pre>

<p><em>This is the client that manages the connection to the socket</em></p>

<pre><code>   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);
            }
        }

    }
</code></pre>

<p><em>This is the endpoint that I create to handle the responses</em></p>

<pre><code>@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();
    }

}
</code></pre>

<java><bitfinex><websockets>
Bitfinex api v2 not returning candles
sfrj
# Bitfinex api v2 not returning candles

(Asked by: username_0 on 2017-05-03)

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();
    }

}

```

**Tags:** java, bitfinex, websockets


---

## 1 answers

### Answer by username_1 on 2018-03-21. Score: 1

The problem might be, that the JSON data does not contain line breaks. I was facing the same issue. My 'subscribe' call had no effect until I added line breaks to the JSON data. I haven't even gotten an error message back from the API.

You could use JSON library for Java which will handle the correct formatting for you.

Please note, there are some existing Libraries for Bitfinex and Java. You don't need to implement the API calls on your own.

* <https://github.com/timmolter/XChange> (this one uses the REST API)
* <https://github.com/jnidzwetzki/bitfinex-v2-wss-api-java> (this one uses the Websocket API)


---

