Skip to content

fix: added null checks for message.data & pong type checking #1027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 27, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ class Realtime(client: Client) : Service(client), CoroutineScope {
val message = text.fromJson<RealtimeResponse>()
when (message.type) {
TYPE_ERROR -> handleResponseError(message)
TYPE_EVENT -> handleResponseEvent(message)
TYPE_EVENT -> message.data?.let {handleResponseEvent(message)}
TYPE_PONG -> {}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions templates/flutter/lib/src/realtime_response.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class RealtimeResponse {

factory RealtimeResponse.fromMap(Map<String, dynamic> map) {
return RealtimeResponse(
type: map['type'],
data: Map<String, dynamic>.from(map['data']),
type: map['type'] ?? '',
data: (map.containsKey('data')) ? Map<String, dynamic>.from(map['data']) : {},
);
}

Expand Down
5 changes: 4 additions & 1 deletion templates/react-native/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Headers = {
}

type RealtimeResponse = {
type: 'error' | 'event' | 'connected' | 'response';
type: 'error' | 'event' | 'connected' | 'response' | 'pong';
data: RealtimeResponseAuthenticated | RealtimeResponseConnected | RealtimeResponseError | RealtimeResponseEvent<unknown>;
}

Expand Down Expand Up @@ -290,6 +290,7 @@ class Client {
this.realtime.lastMessage = message;
switch (message.type) {
case 'event':
if(!message.data) return;
let data = <RealtimeResponseEvent<unknown>>message.data;
if (data?.channels) {
const isSubscribed = data.channels.some(channel => this.realtime.channels.has(channel));
Expand All @@ -301,6 +302,8 @@ class Client {
})
}
break;
case 'pong':
break; // Handle pong response if needed
case 'error':
throw message.data;
default:
Expand Down
2 changes: 1 addition & 1 deletion templates/swift/Sources/Services/Realtime.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extension Realtime: WebSocketClientDelegate {
if let type = json["type"] as? String {
switch type {
case TYPE_ERROR: try! handleResponseError(from: json)
case TYPE_EVENT: handleResponseEvent(from: json)
case TYPE_EVENT: if json["data"] != nil {handleResponseEvent(from: json)}
case TYPE_PONG: break // Handle pong response if needed
default: break
}
Expand Down
3 changes: 3 additions & 0 deletions templates/web/src/client.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class Client {
}
break;
case 'event':
if (!message.data) return;
let data = <RealtimeResponseEvent<unknown>>message.data;
if (data?.channels) {
const isSubscribed = data.channels.some(channel => this.realtime.channels.has(channel));
Expand All @@ -496,6 +497,8 @@ class Client {
})
}
break;
case 'pong':
break; // Handle pong response if needed
case 'error':
throw message.data;
default:
Expand Down
Loading