Pusher connection - socketId is null

1.3k views Asked by At
Pusher pusher = new Pusher(app key);  
String socketId = pusher.getConnection().getSocketId();

The socketId is null when trying to connect to pusher.

The uri the Pusher client is using to make a websocket call is

ws://ws.pusherapp.com:80/app/{app Key}?client=java-client&protocol=5&version=0.3.3

This returns a NULL socket Id

But, if I make a Websocket connection using a test client using the same URI, I get a valid socketId. What am I doing wrong?

1

There are 1 answers

0
leggetter On

The socketId won't be set until the connection has been established. Please see the onConnectionStateChange interface method here: https://github.com/pusher/pusher-websocket-java#api-overview

Here's the code updated specifically to get the socketId:

// Create a new Pusher instance
Pusher pusher = new Pusher(YOUR_APP_KEY);

pusher.connect(new ConnectionEventListener() {
    @Override
    public void onConnectionStateChange(ConnectionStateChange change) {
        String socketId = pusher.getConnection().getSocketId();
        System.out.printLn("The socketId is: " + socketId);
    }

    @Override
    public void onError(String message, String code, Exception e) {
        System.out.println("There was a problem connecting!");
    }
}, ConnectionState.Connected);