From bacc35e8c18312ca2344f8bf3c2ed2e96ff4c630 Mon Sep 17 00:00:00 2001 From: Brian R Date: Tue, 5 Sep 2023 15:47:43 -0400 Subject: [PATCH] Fix, unable to listen postgres changes on channel by making channel options optional. --- Sources/Realtime/Channel.swift | 17 +++-------------- Sources/Realtime/Defaults.swift | 16 ++++++++++++++++ Sources/Realtime/RealtimeClient.swift | 4 ++-- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Sources/Realtime/Channel.swift b/Sources/Realtime/Channel.swift index 0953103..19e01ff 100644 --- a/Sources/Realtime/Channel.swift +++ b/Sources/Realtime/Channel.swift @@ -95,21 +95,10 @@ public class Channel { /// Initialize a Channel /// - parameter topic: Topic of the Channel - /// - parameter options: Optional. Options to configure channel broadcast and presence + /// - parameter options: Optional. Options to configure channel broadcast and presence. Leave nil for postgres channel. /// - parameter socket: Socket that the channel is a part of - convenience init(topic: ChannelTopic, options: ChannelOptions = ChannelOptions(), socket: RealtimeClient) { - let params = [ - "config": [ - "presence": [ - "key": options.presenceKey ?? "" - ], - "broadcast": [ - "ack": options.broadcastAcknowledge, - "self": options.broadcastSelf - ] - ] - ] - self.init(topic: topic, params: params, socket: socket) + convenience init(topic: ChannelTopic, options: ChannelOptions? = nil, socket: RealtimeClient) { + self.init(topic: topic, params: options?.params ?? [:], socket: socket) } /// Initialize a Channel diff --git a/Sources/Realtime/Defaults.swift b/Sources/Realtime/Defaults.swift index 897acd9..7277fec 100644 --- a/Sources/Realtime/Defaults.swift +++ b/Sources/Realtime/Defaults.swift @@ -233,6 +233,22 @@ public struct ChannelOptions { self.broadcastSelf = broadcastSelf self.broadcastAcknowledge = broadcastAcknowledge } + + /// Parameters used to configure the channel + var params: [String: [String: Any]] { + [ + "config": [ + "presence": [ + "key": presenceKey ?? "" + ], + "broadcast": [ + "ack": broadcastAcknowledge, + "self": broadcastSelf + ] + ] + ] + } + } /// Represents the different status of a push diff --git a/Sources/Realtime/RealtimeClient.swift b/Sources/Realtime/RealtimeClient.swift index 578404e..3133a2d 100644 --- a/Sources/Realtime/RealtimeClient.swift +++ b/Sources/Realtime/RealtimeClient.swift @@ -590,11 +590,11 @@ public class RealtimeClient: TransportDelegate { /// let channel = socket.channel("rooms", options: ChannelOptions(presenceKey: "user123")) /// /// - parameter topic: Topic of the channel - /// - parameter options: Optional. Options for the channel + /// - parameter options: Optional. Options to configure channel broadcast and presence. Leave nil for postgres channel. /// - return: A new channel public func channel( _ topic: ChannelTopic, - options: ChannelOptions = ChannelOptions() + options: ChannelOptions? = nil ) -> Channel { let channel = Channel(topic: topic, options: options, socket: self) channels.append(channel)