react-native-nitro-sse 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/android/build.gradle
CHANGED
|
@@ -13,6 +13,9 @@ import okhttp3.sse.EventSourceListener
|
|
|
13
13
|
import okio.Buffer
|
|
14
14
|
import okio.ForwardingSource
|
|
15
15
|
import okio.buffer
|
|
16
|
+
import androidx.lifecycle.DefaultLifecycleObserver
|
|
17
|
+
import androidx.lifecycle.LifecycleOwner
|
|
18
|
+
import androidx.lifecycle.ProcessLifecycleOwner
|
|
16
19
|
import java.util.concurrent.TimeUnit
|
|
17
20
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
18
21
|
import java.util.concurrent.atomic.AtomicLong
|
|
@@ -30,9 +33,11 @@ import kotlin.random.Random
|
|
|
30
33
|
* prevent DoS-ing the server while ensuring resilient reconnections.
|
|
31
34
|
* 4. Heartbeat: Since OkHttp-SSE obscures comments, we use a Network Interceptor to manually
|
|
32
35
|
* detect ':' bytes, enabling JS-side watchdog timers.
|
|
36
|
+
* 5. Lifecycle Management: Implements the Hibernation pattern. When the app enters the background,
|
|
37
|
+
* we stop the stream to save battery. It automatically resumes when returning to foreground.
|
|
33
38
|
*/
|
|
34
39
|
@DoNotStrip
|
|
35
|
-
class NitroSse : HybridNitroSseSpec() {
|
|
40
|
+
class NitroSse : HybridNitroSseSpec(), DefaultLifecycleObserver {
|
|
36
41
|
private var client: OkHttpClient? = null
|
|
37
42
|
private var eventSource: EventSource? = null
|
|
38
43
|
private var config: SseConfig? = null
|
|
@@ -53,6 +58,8 @@ class NitroSse : HybridNitroSseSpec() {
|
|
|
53
58
|
private var reconnectCount = 0
|
|
54
59
|
private var lastErrorTime: Double? = null
|
|
55
60
|
private var lastErrorCode: String? = null
|
|
61
|
+
|
|
62
|
+
private var hasSubscribedToLifecycle = false
|
|
56
63
|
|
|
57
64
|
private val defaultRetryDelayMs = 3000L
|
|
58
65
|
private val baseBackoffDelayMs = 2000L
|
|
@@ -66,46 +73,71 @@ class NitroSse : HybridNitroSseSpec() {
|
|
|
66
73
|
this.config = config
|
|
67
74
|
this.onEventsCallback = onEvent
|
|
68
75
|
|
|
69
|
-
this.client
|
|
70
|
-
.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
override fun
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
76
|
+
if (this.client == null) {
|
|
77
|
+
this.client = OkHttpClient.Builder()
|
|
78
|
+
.connectTimeout(15, TimeUnit.SECONDS)
|
|
79
|
+
.readTimeout(35, TimeUnit.SECONDS)
|
|
80
|
+
.addNetworkInterceptor { chain ->
|
|
81
|
+
val response = chain.proceed(chain.request())
|
|
82
|
+
val responseBody = response.body
|
|
83
|
+
if (responseBody != null) {
|
|
84
|
+
val countingBody = object : ResponseBody() {
|
|
85
|
+
override fun contentType() = responseBody.contentType()
|
|
86
|
+
override fun contentLength() = responseBody.contentLength()
|
|
87
|
+
override fun source() = (object : okio.ForwardingSource(responseBody.source()) {
|
|
88
|
+
override fun read(sink: okio.Buffer, byteCount: Long): Long {
|
|
89
|
+
val bytesRead = super.read(sink, byteCount)
|
|
90
|
+
if (bytesRead != -1L) {
|
|
91
|
+
totalBytesReceived.addAndGet(bytesRead)
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
val snapshot = sink.snapshot()
|
|
95
|
+
if (snapshot.size > 0 && snapshot.get(0) == ':'.toByte()) {
|
|
96
|
+
pushEventToBuffer(SseEvent(SseEventType.HEARTBEAT, null, null, null, "keep-alive"))
|
|
97
|
+
}
|
|
98
|
+
} catch (e: Exception) {
|
|
99
|
+
// Silent catch for interceptor parsing
|
|
89
100
|
}
|
|
90
|
-
} catch (e: Exception) {
|
|
91
|
-
// Silent catch for interceptor parsing
|
|
92
101
|
}
|
|
102
|
+
return bytesRead
|
|
93
103
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
104
|
+
}).buffer()
|
|
105
|
+
}
|
|
106
|
+
response.newBuilder().body(countingBody).build()
|
|
107
|
+
} else {
|
|
108
|
+
response
|
|
97
109
|
}
|
|
98
|
-
response.newBuilder().body(countingBody).build()
|
|
99
|
-
} else {
|
|
100
|
-
response
|
|
101
110
|
}
|
|
102
|
-
|
|
103
|
-
|
|
111
|
+
.build()
|
|
112
|
+
}
|
|
104
113
|
|
|
105
114
|
if (sseHandlerThread == null) {
|
|
106
115
|
sseHandlerThread = android.os.HandlerThread("NitroSseThread").apply { start() }
|
|
107
116
|
sseHandler = Handler(sseHandlerThread!!.looper)
|
|
108
117
|
}
|
|
118
|
+
|
|
119
|
+
if (!hasSubscribedToLifecycle) {
|
|
120
|
+
Handler(Looper.getMainLooper()).post {
|
|
121
|
+
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
|
122
|
+
hasSubscribedToLifecycle = true
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
override fun onStart(owner: LifecycleOwner) {
|
|
128
|
+
if (wasRunningBeforePaused) {
|
|
129
|
+
Log.d(TAG, "App foregrounded. Resuming NitroSse stream.")
|
|
130
|
+
wasRunningBeforePaused = false
|
|
131
|
+
start()
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
override fun onStop(owner: LifecycleOwner) {
|
|
136
|
+
if (isRunning.get()) {
|
|
137
|
+
Log.d(TAG, "App backgrounded. Hibernating NitroSse connection.")
|
|
138
|
+
wasRunningBeforePaused = true
|
|
139
|
+
stop()
|
|
140
|
+
}
|
|
109
141
|
}
|
|
110
142
|
|
|
111
143
|
private fun pushEventToBuffer(event: SseEvent) {
|
|
@@ -163,7 +195,7 @@ class NitroSse : HybridNitroSseSpec() {
|
|
|
163
195
|
}
|
|
164
196
|
|
|
165
197
|
override fun start() {
|
|
166
|
-
if (config == null) return
|
|
198
|
+
if (config == null || isRunning.get()) return
|
|
167
199
|
isRunning.set(true)
|
|
168
200
|
backoffCounter = 0
|
|
169
201
|
sseHandler?.post { performConnection() }
|
|
@@ -172,6 +204,10 @@ class NitroSse : HybridNitroSseSpec() {
|
|
|
172
204
|
private fun performConnection() {
|
|
173
205
|
if (!isRunning.get()) return
|
|
174
206
|
|
|
207
|
+
// Cancel existing event source if any before starting new one
|
|
208
|
+
eventSource?.cancel()
|
|
209
|
+
eventSource = null
|
|
210
|
+
|
|
175
211
|
val requestBuilder = Request.Builder()
|
|
176
212
|
.url(config!!.url)
|
|
177
213
|
.header("Accept", "text/event-stream")
|
package/ios/NitroSse.swift
CHANGED
|
@@ -43,6 +43,7 @@ class NitroSse: HybridNitroSseSpec, EventHandler {
|
|
|
43
43
|
self.config = config
|
|
44
44
|
self.onEventsCallback = onEvent
|
|
45
45
|
|
|
46
|
+
NotificationCenter.default.removeObserver(self)
|
|
46
47
|
NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
|
|
47
48
|
NotificationCenter.default.addObserver(self, selector: #selector(handleAppWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
|
|
48
49
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-sse",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "High-performance Server-Sent Events (SSE) for React Native, powered by Nitro Modules.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|