Search code examples
javaprocessingpubnub

pubnub processing/java for a noob


I am using processing (IE, JAVA) to publish via pubnub. After reading nearly all the simple startup documentation, I am at a loss. My code is running, but not publishing anything. I have the libraries installed correctly I believe, but no messages are published via my keys.

I know I am just not understanding something really simple here, but I have no idea what that might be. I added a 5s delay to ensure everything finishes before setup stops as suggested in other posts. Right now it runs, but no messages show up in the app with those keys(which I replaced below). All the examples are way over my head and not helping my poor noob brain.

I also setup the same basic code in Python which also runs but does not actually publish the message. Leading me to think I am just missing something very fundamental.

In the end, I want to push data from a UI to an online real-time dashboard. This seems trivially easy, but it is a complete black hole.

Here is my code (it's basically just a copy and paste from the startup code):

import com.pubnub.api.*;
import org.json.*;

Pubnub pubnub = new Pubnub("PUBKEY", "SUBKEY");

void setup()
{
  try {
    pubnub.subscribe("NSFPhaseII", new Callback() {
      @Override
      public void connectCallback(String channel, Object message) {
        pubnub.publish("NSFPhaseII", "Hello from the PubNub Java SDK", new Callback() {});
      }

      @Override
      public void disconnectCallback(String channel, Object message) {
        System.out.println("SUBSCRIBE : DISCONNECT on channel:" + channel
          + " : " + message.getClass() + " : "
          + message.toString());
      }

      public void reconnectCallback(String channel, Object message) {
        System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel
          + " : " + message.getClass() + " : "
          + message.toString());
      }

      @Override
      public void successCallback(String channel, Object message) {
        System.out.println("SUBSCRIBE : " + channel + " : "
          + message.getClass() + " : " + message.toString());
      }

      @Override
      public void errorCallback(String channel, PubnubError error) {
        System.out.println("SUBSCRIBE : ERROR on channel " + channel
          + " : " + error.toString());
      }
    });
  } 
  catch (PubnubException e) {
    System.out.println(e.toString());
  }

  delay(5000);
  println("done");
}

Solution

  • I am answering my own question here.

    It turns out this code has been working all along. I setup two processing sketches in a publish subscribe type setup and could clearly see that one was receiving the publishes from the other.

    Then, when I checked on pubnub, the messages registered in my account. I am not really sure if the messages were registering in my account all along and just not updating or if they took some time, but I believe it was working all along and I was just not waiting long enough to see the messages register online.

    Like I said. Noobs...