SelectPubNub Scala SDK 3.7.11




Add PubNub to your project using one of the procedures defined under How to Get It.
 import com.pubnub.api.{Pubnub, Callback, PubnubError} 
val pubnub = new Pubnub("demo","demo")

pubnub.subscribe("my_channel", new Callback {
  override def connectCallback(channel: String, message: Object) {
    pubnub.publish("my_channel", "Hello from the PubNub Scala SDK", new Callback() {})
  }
})
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance, and the secret_key if you wish to perform PAM administrative operations from this Scala instance.
 

For security reasons you should only include the secret-key on a highly secured server. The secret-key is only required for granting rights using our Access Manager.

When you init with secret_key, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

val pubnub = new Pubnub("demo","demo")
 
If you no longer need to use the Pubnub instance, do not forget to explicitly stop it's running threads using shutdown() method.
Call time() to verify the client connectivity to the origin:
val callback = new Callback() {
  override def successCallback(channel: String, response: Object): Unit =
    println(response)

  override def errorCallback(channel: String, pubnubError: PubnubError): Unit =
    println(pubnubError.getErrorString)
}

pubnub.time(callback)
Subscribe (listen on) a channel (it's async!):
pubnub.subscribe("my_channel", new Callback {
  override def connectCallback(channel: String, message: Object): Unit =
    println("SUBSCRIBE : CONNECT on channel: " + channel + " : " + message.getClass +
      " : " + message.toString)

  override def disconnectCallback(channel: String, message: Object): Unit =
    println("SUBSCRIBE : DISCONNECT on channel: " + channel + " : " + message.getClass +
      " : " + message.toString)

  override def successCallback(channel: String, message: Object): Unit =
    println("SUBSCRIBE :" + channel + " : " + message.getClass + " : " + message)

  override def errorCallback(channel: String, pubnubError: PubnubError): Unit =
    println("SUBSCRIBE : ERROR on channel:" + channel + " : " + pubnubError)

  override def reconnectCallback(channel: String, message: Object): Unit =
    println("SUBSCRIBE : RECONNECT on channel:" + channel + " : " + message.getClass +
      " : " + message.toString)
})
Publish a message to a channel:
val callback = new Callback() {
  override def successCallback(channel: String, response: Object): Unit =
    println(response.toString)

  override def errorCallback(channel: String, pubnubError: PubnubError): Unit =
    println(pubnubError.getErrorString)
}

pubnub.publish("my_channel", "Hello from the PubNub Java SDK!", callback)
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
val callback = new Callback {
  override def successCallback(channel: String, message: Object): Unit =
    println(message)

  override def errorCallback(channel: String, pubnubError: PubnubError): Unit =
    println(pubnubError.getErrorString)
}

pubnub.hereNow("my_channel", callback)
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.presence("my_channel", new Callback {
  override def connectCallback(channel: String, message: Object): Unit =
    println("CONNECT on channel: " + channel + " : " + message.getClass +
      " : " + message.toString)

  override def disconnectCallback(channel: String, message: Object): Unit =
    println("DISCONNECT on channel: " + channel + " : " + message.getClass +
      " : " + message.toString)

  override def successCallback(channel: String, message: Object): Unit =
    println(channel + " : " + message.getClass + " : " + message)

  override def errorCallback(channel: String, pubnubError: PubnubError): Unit =
    println(ERROR on channel:" + channel + " : " + pubnubError)

  override def reconnectCallback(channel: String, message: Object): Unit =
    println(RECONNECT on channel:" + channel + " : " + message.getClass +
      " : " + message.toString)
})
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.history("history_channel", 100, true, new Callback {
  override def successCallback(channel: String, message: Object): Unit =
    println(message.toString)

  override def errorCallback(channel: String, pubnubError: PubnubError): Unit =
    println(pubnubError.getErrorString)
})
Stop subscribing (listening) to a channel.
pubnub.unsubscribe("my_channel")