Skip to content

Commit 085f227

Browse files
committed
Fix socket creation in Realtime.kt
The socket was being created unnecessarily whenever a subscription was removed. This change ensures that the socket is only created when there are active channels.
1 parent 6b9a5b3 commit 085f227

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

templates/kmp/shared/src/commonMain/kotlin/io/package/BaseClient.kt.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,14 @@ abstract class BaseClient<This : BaseClient<This>>(
361361

362362
if (response.contentType()?.match(ContentType.Application.Json) == true) {
363363
val map = json.decodeFromString<Map<String, JsonElement>>(body)
364-
throw AppwriteException(
364+
throw {{ spec.title | caseUcfirst }}Exception(
365365
(map["message"] as? JsonPrimitive)?.content ?: "",
366366
(map["code"] as? JsonPrimitive)?.int ?: 0,
367367
(map["type"] as? JsonPrimitive)?.content ?: "",
368368
body
369369
)
370370
} else {
371-
throw AppwriteException(body, response.status.value)
371+
throw {{ spec.title | caseUcfirst }}Exception(body, response.status.value)
372372
}
373373
}
374374

@@ -383,7 +383,7 @@ abstract class BaseClient<This : BaseClient<This>>(
383383
body.isEmpty() -> when (responseType) {
384384
String::class -> "" as T
385385
Boolean::class -> true as T
386-
else -> throw AppwriteException(
386+
else -> throw {{ spec.title | caseUcfirst }}Exception(
387387
"Empty response body",
388388
response.status.value
389389
)
@@ -394,7 +394,7 @@ abstract class BaseClient<This : BaseClient<This>>(
394394
serializer?.let { json.decodeFromString(it, body) }
395395
?: json.decodeFromString(responseType.serializer(), body)
396396
} catch (e: Exception) {
397-
throw AppwriteException(
397+
throw {{ spec.title | caseUcfirst }}Exception(
398398
"Failed to parse JSON response: ${e.message}",
399399
response.status.value,
400400
response = body

templates/kmp/shared/src/commonMain/kotlin/io/package/services/Realtime.kt.twig

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package {{ sdk.namespace | caseDot }}.services
22

33
import {{ sdk.namespace | caseDot }}.Client
44
import {{ sdk.namespace | caseDot }}.Service
5-
import {{ sdk.namespace | caseDot }}.exceptions.AppwriteException
5+
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
66
import {{ sdk.namespace | caseDot }}.extensions.forEachAsync
77
import {{ sdk.namespace | caseDot }}.extensions.fromJson
88
import {{ sdk.namespace | caseDot }}.extensions.getSerializer
@@ -53,32 +53,31 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
5353

5454
private fun createSocket() {
5555
launch(Dispatchers.IO) {
56+
try {
5657

57-
if (activeChannels.isEmpty()) {
58-
reconnect = false
59-
closeSocket()
60-
return@launch
61-
}
62-
63-
val queryParams = buildString {
64-
append("project=${client.config["project"]}")
65-
activeChannels.forEach {
66-
append("&channels[]=$it")
58+
if (activeChannels.isEmpty()) {
59+
reconnect = false
60+
closeSocket()
61+
return@launch
6762
}
68-
}
6963

70-
if (webSocketSession != null) {
71-
reconnect = false
72-
closeSocket()
73-
}
64+
val queryParams = buildString {
65+
append("project=${client.config["project"]}")
66+
activeChannels.forEach {
67+
append("&channels[]=$it")
68+
}
69+
}
7470

75-
webSocketSession = client.httpClient.webSocketSession {
76-
url("${client.endpointRealtime}/realtime?$queryParams")
77-
}
71+
if (webSocketSession != null) {
72+
reconnect = false
73+
closeSocket()
74+
}
7875

79-
reconnectAttempts = 0 // Reset attempts on successful connection
76+
webSocketSession = client.httpClient.webSocketSession {
77+
url("${client.endpointRealtime}/realtime?$queryParams")
78+
}
8079

81-
try {
80+
reconnectAttempts = 0 // Reset attempts on successful connection
8281
webSocketSession?.let { session ->
8382
for (frame in session.incoming) {
8483
when (frame) {
@@ -87,6 +86,7 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
8786
}
8887
}
8988
}
89+
9090
} catch (e: Exception) {
9191
handleFailure(e)
9292
} finally {
@@ -149,7 +149,6 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
149149
return RealtimeSubscription {
150150
activeSubscriptions.remove(counter)
151151
cleanUp(channels)
152-
createSocket()
153152
}
154153
}
155154

@@ -174,7 +173,7 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
174173
}
175174

176175
private fun handleResponseError(message: RealtimeResponse) {
177-
throw message.data.jsonCast<AppwriteException>()
176+
throw message.data.jsonCast<{{ spec.title | caseUcfirst }}Exception>()
178177
}
179178

180179
@Throws(Throwable::class)

0 commit comments

Comments
 (0)