JavaJava V3 to Java V4 Migration Guide

  • V3 exposes PubNub
  • V4 exposes PubNub and PNConfiguration
  • V3 initiates via:
    final Pubnub pubnub = new Pubnub("PUB_KEY", "SUB_KEY");
    pubnub.setUUID(UUID.randomUUID().toString());
    pubnub.setSubscribeTimeout(200);
  • V4 initiates via:
    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.setSubscribeKey("SUB_KEY");
    pnConfiguration.setPublishKey("PUB_KEY");
    pnConfiguration.setUuid(UUID.randomUUID().toString());
    pnConfiguration.setPresenceTimeout(200);
    PubNub pubnub = new PubNub(pnConfiguration);
The V4 SDK changes how PubNub handles callbacks. Instead of instantiating a com.pubnub.api.Callback instance every time when executing a PubNub operation, the SDK introduces a new class SubscribeCallback, which can be further extended and instantiated in a global manner. Furthermore, it receives various messages from PubNub, e.g. state and presence events, as well as published messages.
  • public class MySubscribeCallback extends SubscribeCallback {
        @Override
        public void status(PubNub pubnub, PNStatus status) {
    
        }
    
        @Override
        public void message(PubNub pubnub, PNMessageResult message) {
    
        }
    
        @Override
        public void presence(PubNub pubnub, PNPresenceEventResult presence) {
    
        }
    }
  • MySubscribeCallback mySubscribeCallback = new MySubscribeCallback();
    pubnub.addListener(mySubscribeCallback);
    pubnub.removeListener(mySubscribeCallback); // some time later in the app
  • V3 subscribe:
    try {
        pubnub.subscribe(newString[]{"channel1"}, new Callback() {
            @Override
            public void successCallback(String channel, Object message) {
    
            }
    
            @Override
            public void errorCallback(String channel, PubnubError error) {
    
            }     
        });
    }
    catch (PubnubException e) {
        e.printStackTrace();
    }
  • V4 subscribe:
    pubnub.subscribe()
        .channels(Arrays.asList("channel_1"))
        .execute();
  • V3 unsubscribe:
    pubnub.unsubscribe(newString[]{"channel_1"}, new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 unsubscribe
    pubnub.unsubscribe()
        .channels(Arrays.asList("channel_1"))
        .execute();
  • V3 publish
    pubnub.publish("channel_1", "Hello from the PubNub Java SDK V3!", new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
        
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 publish
    pubnub.publish()
        .channel("channel_1")
        .message("Hello from the PubNub Java SDK V4!")
        .async(new PNCallback<PNPublishResult>() {
            @Override
            public void onResponse(PNPublishResult result, PNStatus status) {
    
            }
        });
  • V3 here now:
    pubnub.hereNow("channel_1", new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 here now:
    pubnub.hereNow()
        .channels(Arrays.asList("channel_1"))
        .async(new PNCallback<PNHereNowResult>() {
            @Override
            public void onResponse(PNHereNowResult result, PNStatus status) {
            }
        });
  • V3 presence:
    try {
        pubnub.presence("channel_1", new Callback() {
            @Override
            public void successCallback(String channel, Object message) {
                super.successCallback(channel, message);
            }
    
            @Override
            public void errorCallback(String channel, PubnubError error) {
                super.errorCallback(channel, error);
            }
        });
    }
    catch (PubnubException e) {
        e.printStackTrace();
    }
  • V4 presence:
    pubnub.subscribe()
        .channels(Arrays.asList("channel_1"))
        .withPresence() // add this line when subscribing
        .execute();
  • V3 history:
    pubnub.history("channel_1", 100, new Callback() {
        @Override 
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 history:
    pubnub.history()
        .channel("channel_1")
        .count(100)
        .async(newPNCallback<PNHistoryResult>() {
            @Override
            public void onResponse(PNHistoryResult result, PNStatus status) {
            
            }
        });
  • V3 set state:
    JSONObject stateJson = new JSONObject(); 
    try {
        stateJson.put("my_state_message", "Hello from the PubNub Java SDK V3!");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    pubnub.setState("channel_1", "device_uuid", stateJson, new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 set state:
    JSONObject stateJson = new JSONObject();
    try {
        stateJson.put("my_state_message", "Hello from the PubNub Java SDK V4!");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    pubnub.setPresenceState()
        .channels(Arrays.asList("channel_1"))
        .state(stateJson)
        .async(new PNCallback<PNSetStateResult>() {
            @Override
            public void onResponse(PNSetStateResult result, PNStatus status) {
            
            }
        });
  • V3 get state:
    pubnub.getState("channel_1", "device_uuid", new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 get state:
    pubnub.getPresenceState()
        .channels(Arrays.asList("channel_1"))
        .uuid("device_uuid")
        .async(new PNCallback<PNGetStateResult>() {
            @Override
            public void onResponse(PNGetStateResult result, PNStatus status) {
    
            }
        });
  • V3 subscribe:
    pubnub.enablePushNotificationsOnChannel("channel_1", "fcm_token", new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 subscribe:
    pubnub.addPushNotificationsOnChannels()
        .channels(Arrays.asList("channel_1"))
        .deviceId("fcm_token")
        .pushType(PNPushType.GCM)
        .async(new PNCallback<PNPushAddChannelResult>() {
    
            @Override
            public void onResponse(PNPushAddChannelResult result, PNStatus status) {
    
            }
        });
  • V3 unsubscribe:
    pubnub.disablePushNotificationsOnChannel("channel_1", "fcm_token", new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 unsubscribe:
    pubnub.removePushNotificationsFromChannels()
        .channels(Arrays.asList("channel_1"))
        .deviceId("fcm_token")
        .pushType(PNPushType.GCM)
        .async(new PNCallback<PNPushRemoveChannelResult>() {
            @Override
            public void onResponse(PNPushRemoveChannelResult result, PNStatus status) {
    
            }
        });
  • V3 time
    pubnub.time(new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            super.successCallback(channel, message);
        }
    
        @Override
        public void errorCallback(String channel, PubnubError error) {
            super.errorCallback(channel, error);
        }
    });
  • V4 time
    pubnub.time()
        .async(newPNCallback<PNTimeResult>() {
            @Override
            public void onResponse(PNTimeResult result, PNStatus status) {
    
            }
        });
  • V3 destroy
    pubnub.shutdown();
  • V4 destroy
    pubnub.destroy();