Skip to content

Commit 31e850c

Browse files
authored
Mc/fix readme (#342)
* Updated README with new event listener examples.
1 parent b751a94 commit 31e850c

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

README.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,68 @@ You will need the publish and subscribe keys to authenticate your app. Get your
3939
```
4040

4141
## Add event listeners
42+
```kotlin
43+
// Create a subscription to a specific channel
44+
val subscription = pubnub.channel("my_channel").subscription(SubscriptionOptions.receivePresenceEvents())
45+
46+
// Add a listener to the subscription for handling various event types
47+
subscription.addListener(object : EventListener {
48+
override fun message(pubnub: PubNub, message: PNMessageResult) {
49+
// Log or process message
50+
println("Message: ${message.message}")
51+
}
52+
53+
override fun signal(pubnub: PubNub, signal: PNSignalResult) {
54+
// Handle signals
55+
println("Signal: ${signal.message}")
56+
}
57+
58+
override fun messageAction(pubnub: PubNub, messageAction: PNMessageActionResult) {
59+
// Handle message reactions
60+
println("Message Reaction: ${messageAction.data}")
61+
}
62+
63+
override fun file(pubnub: PubNub, file: PNFileEventResult) {
64+
// Handle file events
65+
println("File: ${file.file.name}")
66+
}
67+
68+
override fun objects(pubnub: PubNub, obj: PNObjectEventResult) {
69+
// Handle metadata updates
70+
println("App Context: ${obj.event}")
71+
}
72+
73+
override fun presence(pubnub: PubNub, presence: PNPresenceEventResult) {
74+
// Handle presence updates
75+
// requires a subscription with presence
76+
println("Presence: ${presence.uuid} - ${presence.event}")
77+
}
78+
})
79+
80+
// Adding the status listener to the PubNub client
81+
pubnub.addListener(object : StatusListener() {
82+
override fun status(pubnub: PubNub, status: PNStatus) {
83+
// This block is executed asynchronously for each status update
84+
println("Connection Status: ${status.category}")
85+
}
86+
})
87+
88+
// for subscription set
89+
val subscriptionSet = pubnub.subscriptionSetOf(
90+
// Specify channels with default options
91+
channels = setOf("my_channel", "other_channel"),
92+
)
93+
94+
subscriptionSet.addListener(object : EventListener {
95+
override fun message(pubnub: PubNub, message: PNMessageResult) {
96+
// Log or process message
97+
println("Message: ${message.message}")
98+
}
99+
})
100+
101+
```
42102

103+
#### Add event listeners old way
43104
```kotlin
44105
pubnub.addListener(object : SubscribeCallback() {
45106

@@ -92,8 +153,29 @@ pubnub.addListener(object : SubscribeCallback() {
92153
})
93154
```
94155

95-
## Publish/subscribe
156+
## Subscribe
157+
```kotlin
158+
subscription.subscribe()
159+
```
160+
#### Subscribe old way
161+
```kotlin
162+
pubnub.subscribe(channels = listOf("my_channel"), withPresence = true)
163+
```
164+
165+
## Publish
166+
```kotlin
167+
val channel = pubnub.channel("my_channel")
168+
channel.publish(message = "hello").async { result ->
169+
result.onSuccess { response ->
170+
println("Message timetoken: ${response.timetoken}")
171+
}.onFailure { exception ->
172+
println("ERROR: Failed to publish message")
173+
println("Error details: ${exception.message}")
174+
}
175+
}
96176

177+
```
178+
#### Publish old way
97179
```kotlin
98180
pubnub.publish(channel = "my_channel", message = "hello")
99181
.async { result ->

0 commit comments

Comments
 (0)