Skip to content

Commit 0ec7faf

Browse files
fix: event name conflicts with other libs using RCTDeviceEventEmitter (#70)
* Create PusherEventEmitter on Android with event prefix * Use PusherReactNative event prefix on iOS events * Add package locks to the repo * Bump to version 1.2.1 Co-authored-by: Pusher CI <pusher-ci@pusher.com>
1 parent 393b112 commit 0ec7faf

File tree

8 files changed

+21290
-46
lines changed

8 files changed

+21290
-46
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.2.1
4+
5+
* [FIXED] Fixed event name conflicts with other libs using RCTDeviceEventEmitter
6+
37
## 1.2.0
48

59
* [CHANGED] Remove mutex locks in favor of storing callbacks so onAuthorizer does no longer freeze the app on iOS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.pusherwebsocketreactnative
2+
3+
import com.facebook.react.bridge.Arguments
4+
import com.facebook.react.bridge.ReactApplicationContext
5+
import com.facebook.react.modules.core.DeviceEventManagerModule
6+
7+
class PusherEventEmitter(private val context: ReactApplicationContext) {
8+
companion object {
9+
private const val EVENT_PREFIX = "PusherReactNative"
10+
}
11+
12+
fun emit(eventName: String, params: Any?) {
13+
val jsModule = this.context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
14+
val pusherEventName = "${EVENT_PREFIX}:${eventName}"
15+
16+
if (params is Map<*, *>) {
17+
jsModule.emit(pusherEventName, Arguments.makeNativeMap(params as Map<String, Any>))
18+
}
19+
20+
if (params is String) {
21+
jsModule.emit(pusherEventName, params)
22+
}
23+
}
24+
}

android/src/main/java/com/pusherwebsocketreactnative/PusherWebsocketReactNativeModule.kt

+12-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.pusherwebsocketreactnative
22

33
import android.util.Log
44
import com.facebook.react.bridge.*
5-
import com.facebook.react.modules.core.DeviceEventManagerModule
65
import com.google.gson.Gson
76
import com.pusher.client.ChannelAuthorizer
87
import com.pusher.client.Pusher
@@ -27,22 +26,12 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
2726
private val authorizerMutex = mutableMapOf<String, Semaphore>()
2827
private val authorizerResult = mutableMapOf<String, ReadableMap>()
2928

29+
private val pusherEventEmitter = PusherEventEmitter(reactContext)
30+
3031
override fun getName(): String {
3132
return "PusherWebsocketReactNative"
3233
}
3334

34-
private fun emitEvent(eventName: String, params: Any?) {
35-
val jsModule = this.reactApplicationContext
36-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
37-
if (params is Map<*, *>) {
38-
jsModule.emit(eventName, Arguments.makeNativeMap(params as Map<String, Any>))
39-
}
40-
41-
if (params is String) {
42-
jsModule.emit(eventName, params)
43-
}
44-
}
45-
4635
@ReactMethod
4736
fun addListener(eventName: String?) {
4837
// Keep: Required for RN built in Event Emitter Calls.
@@ -148,7 +137,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
148137
}
149138

150139
override fun authorize(channelName: String, socketId: String): String? {
151-
emitEvent(
140+
pusherEventEmitter.emit(
152141
"onAuthorizer", mapOf(
153142
"channelName" to channelName,
154143
"socketId" to socketId
@@ -173,7 +162,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
173162

174163
// Event handlers
175164
override fun onConnectionStateChange(change: ConnectionStateChange) {
176-
emitEvent(
165+
pusherEventEmitter.emit(
177166
"onConnectionStateChange", mapOf(
178167
"previousState" to change.previousState.toString(),
179168
"currentState" to change.currentState.toString()
@@ -184,7 +173,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
184173
override fun onSubscriptionSucceeded(channelName: String) {
185174
// For presence channels we wait for the onUsersInformationReceived event.
186175
if (!channelName.startsWith("presence-")) {
187-
emitEvent(
176+
pusherEventEmitter.emit(
188177
"onEvent", mapOf(
189178
"channelName" to channelName,
190179
"eventName" to "pusher_internal:subscription_succeeded",
@@ -195,7 +184,6 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
195184
}
196185

197186
override fun onEvent(event: PusherEvent) {
198-
// Log.i(TAG, "Received event with data: $event")
199187
// The java sdk transforms some events from pusher_internal
200188
// to pusher:... events, we translate them back.
201189
val finalEvent = if (event.eventName === "pusher:subscription_count") {
@@ -207,7 +195,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
207195
} else {
208196
event
209197
}
210-
emitEvent(
198+
pusherEventEmitter.emit(
211199
"onEvent", mapOf(
212200
"channelName" to finalEvent.channelName,
213201
"eventName" to finalEvent.eventName,
@@ -218,8 +206,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
218206
}
219207

220208
override fun onAuthenticationFailure(message: String, e: Exception) {
221-
// Log.e(TAG, "Authentication failure due to $message, exception was $e")
222-
emitEvent(
209+
pusherEventEmitter.emit(
223210
"onSubscriptionError", mapOf(
224211
"message" to message,
225212
"error" to e.toString()
@@ -228,7 +215,6 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
228215
} // Other ChannelEventListener methods
229216

230217
override fun onUsersInformationReceived(channelName: String?, users: MutableSet<User>?) {
231-
// Log.i(TAG, "Users received: $users")
232218
val gson = Gson()
233219
val channel = pusher!!.getPresenceChannel(channelName)
234220
val hash = mutableMapOf<String, Any?>()
@@ -243,7 +229,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
243229
"hash" to hash
244230
)
245231
)
246-
emitEvent(
232+
pusherEventEmitter.emit(
247233
"onEvent", mapOf(
248234
"channelName" to channelName,
249235
"eventName" to "pusher_internal:subscription_succeeded",
@@ -254,8 +240,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
254240
}
255241

256242
override fun onDecryptionFailure(event: String?, reason: String?) {
257-
// Log.e(TAG, "Decryption failure due to $event, exception was $reason")
258-
emitEvent(
243+
pusherEventEmitter.emit(
259244
"onDecryptionFailure", mapOf(
260245
"event" to event,
261246
"reason" to reason
@@ -264,9 +249,8 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
264249
}
265250

266251
override fun userSubscribed(channelName: String, user: User) {
267-
// Log.i(TAG, "A new user joined channel [$channelName]: ${user.id}, ${user.info}")
268252
val gson = Gson()
269-
emitEvent(
253+
pusherEventEmitter.emit(
270254
"onMemberAdded", mapOf(
271255
"channelName" to channelName,
272256
"user" to mapOf(
@@ -278,9 +262,8 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
278262
}
279263

280264
override fun userUnsubscribed(channelName: String, user: User) {
281-
// Log.i(TAG, "A user left channel [$channelName]: ${user.id}, ${user.info}")
282265
val gson = Gson()
283-
emitEvent(
266+
pusherEventEmitter.emit(
284267
"onMemberRemoved", mapOf(
285268
"channelName" to channelName,
286269
"user" to mapOf(
@@ -292,7 +275,7 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
292275
} // Other ChannelEventListener methods
293276

294277
override fun onError(message: String, code: String?, e: Exception?) {
295-
emitEvent(
278+
pusherEventEmitter.emit(
296279
"onError", mapOf(
297280
"message" to message,
298281
"code" to code,

0 commit comments

Comments
 (0)