|
6 | 6 | [](https://bintray.com/bintray/jcenter/com.pubnub%3Apubnub-kotlin/_latestVersion)
|
7 | 7 | []()
|
8 | 8 |
|
9 |
| -### [Documentation](https://www.pubnub.com/docs/kotlin-java/pubnub-java-sdk) |
| 9 | +This is the official PubNub Kotlin SDK repository. |
10 | 10 |
|
11 |
| -## Communication |
| 11 | +PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms. |
12 | 12 |
|
13 |
| -- If you **need help ** or have a **general question **, contact <[email protected]> |
| 13 | +## Get keys |
| 14 | + |
| 15 | +You will need the publish and subscribe keys to authenticate your app. Get your keys from the [Admin Portal](https://dashboard.pubnub.com/login). |
| 16 | + |
| 17 | +## Configure PubNub |
| 18 | + |
| 19 | +1. Integrate the Kotlin SDK into your project: |
| 20 | + |
| 21 | + * for Maven, add the following dependency in your `pom.xml`: |
| 22 | + ```xml |
| 23 | + <dependency> |
| 24 | + <groupId>com.pubnub</groupId> |
| 25 | + <artifactId>pubnub-gson</artifactId> |
| 26 | + <version>5.0.2</version> |
| 27 | + </dependency> |
| 28 | + ``` |
| 29 | + |
| 30 | + * for Gradle, add the following dependency in your `gradle.build`: |
| 31 | + ```groovy |
| 32 | + implementation 'com.pubnub:pubnub-kotlin:5.0.2' |
| 33 | + ``` |
| 34 | + |
| 35 | +2. Configure your keys: |
| 36 | + |
| 37 | + ```kotlin |
| 38 | + val config = PNConfiguration().apply{ |
| 39 | + subscribeKey = "mySubKey" |
| 40 | + publishKey = "myPubKey" |
| 41 | + uuid = "myUniqueUuid" |
| 42 | + } |
| 43 | + ``` |
| 44 | + |
| 45 | +## Add event listeners |
| 46 | + |
| 47 | +```kotlin |
| 48 | +pubnub.addListener(object : SubscribeCallback() { |
| 49 | + |
| 50 | + override fun status(pubnub: PubNub, status: PNStatus) { |
| 51 | + println("Status category: ${status.category}") |
| 52 | + // PNConnectedCategory, PNReconnectedCategory, PNDisconnectedCategory |
| 53 | + |
| 54 | + println("Status operation: ${status.operation}") |
| 55 | + // PNSubscribeOperation, PNHeartbeatOperation |
| 56 | + |
| 57 | + println("Status error: ${status.error}") |
| 58 | + // true or false |
| 59 | + } |
| 60 | + |
| 61 | + override fun presence(pubnub: PubNub, pnPresenceEventResult: PNPresenceEventResult) { |
| 62 | + println("Presence event: ${pnPresenceEventResult.event}") |
| 63 | + println("Presence channel: ${pnPresenceEventResult.channel}") |
| 64 | + println("Presence uuid: ${pnPresenceEventResult.uuid}") |
| 65 | + println("Presence timetoken: ${pnPresenceEventResult.timetoken}") |
| 66 | + println("Presence occupancy: ${pnPresenceEventResult.occupancy}") |
| 67 | + } |
| 68 | + |
| 69 | + override fun message(pubnub: PubNub, pnMessageResult: PNMessageResult) { |
| 70 | + println("Message payload: ${pnMessageResult.message}") |
| 71 | + println("Message channel: ${pnMessageResult.channel}") |
| 72 | + println("Message publisher: ${pnMessageResult.publisher}") |
| 73 | + println("Message timetoken: ${pnMessageResult.timetoken}") |
| 74 | + } |
| 75 | + |
| 76 | + override fun signal(pubnub: PubNub, pnSignalResult: PNSignalResult) { |
| 77 | + println("Signal payload: ${pnSignalResult.message}") |
| 78 | + println("Signal channel: ${pnSignalResult.channel}") |
| 79 | + println("Signal publisher: ${pnSignalResult.publisher}") |
| 80 | + println("Signal timetoken: ${pnSignalResult.timetoken}") |
| 81 | + } |
| 82 | + |
| 83 | + override fun messageAction(pubnub: PubNub, pnMessageActionResult: PNMessageActionResult) { |
| 84 | + with(pnMessageActionResult.messageAction) { |
| 85 | + println("Message action type: $type") |
| 86 | + println("Message action value: $value") |
| 87 | + println("Message action uuid: $uuid") |
| 88 | + println("Message action actionTimetoken: $actionTimetoken") |
| 89 | + println("Message action messageTimetoken: $messageTimetoken") |
| 90 | + } |
| 91 | + |
| 92 | + println("Message action subscription: ${pnMessageActionResult.subscription}") |
| 93 | + println("Message action channel: ${pnMessageActionResult.channel}") |
| 94 | + println("Message action timetoken: ${pnMessageActionResult.timetoken}") |
| 95 | + } |
| 96 | +}) |
| 97 | +``` |
| 98 | + |
| 99 | +## Publish/subscribe |
| 100 | + |
| 101 | +```kotlin |
| 102 | +pubnub.publish(channel = "my_channel", message = "hello") |
| 103 | + .async { result, status -> |
| 104 | + // the result is always of a nullable type |
| 105 | + // it's null if there were errors (status.error) |
| 106 | + // otherwise it's usable |
| 107 | + |
| 108 | + // handle publish result |
| 109 | + if (!status.error) { |
| 110 | + println("Message timetoken: ${result!!.timetoken}") |
| 111 | + } else { |
| 112 | + // handle error |
| 113 | + status.exception.printStackTrace() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +pubnub.subscribe(channels = listOf("my_channel")) |
| 118 | +``` |
| 119 | + |
| 120 | +## Documentation |
| 121 | + |
| 122 | +* [API reference for Kotlin ](https://www.pubnub.com/docs/kotlin-java/pubnub-java-sdk) |
| 123 | + |
| 124 | +## Support |
| 125 | + |
| 126 | +If you **need help ** or have a **general question **, contact [email protected]. |
0 commit comments