Skip to content

Commit 7b91c66

Browse files
committed
Docs code snippets
1 parent 7c07f7e commit 7b91c66

10 files changed

+3101
-1
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
//
2+
// ChannelGroupSnippets.swift
3+
//
4+
// Copyright (c) PubNub Inc.
5+
// All rights reserved.
6+
//
7+
// This source code is licensed under the license found in the
8+
// LICENSE file in the root directory of this source tree.
9+
//
10+
11+
import PubNubSwiftChatSDK
12+
13+
var chat: ChatImpl!
14+
var channelGroup: ChannelGroupImpl!
15+
var supportChannel: ChannelImpl!
16+
var generalChannel: ChannelImpl!
17+
var autoCloseable: AutoCloseable!
18+
19+
// MARK: - Get Channel Group Reference
20+
21+
func getChannelGroupReference() {
22+
// snippet.channelGroups.getReference
23+
// Assumes a "ChatImpl" reference named "chat"
24+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
25+
// snippet.end
26+
}
27+
28+
// MARK: - Remove Channel Group
29+
30+
func removeChannelGroup() {
31+
// snippet.channelGroups.remove
32+
// Assumes a "ChatImpl" reference named "chat"
33+
Task {
34+
try await chat.removeChannelGroup(id: "my-channel-group")
35+
debugPrint("Channel group removed successfully")
36+
}
37+
// snippet.end
38+
}
39+
40+
// MARK: - List Channels
41+
42+
func listChannelsInGroup() {
43+
// snippet.channelGroups.listChannels
44+
// Assumes a "ChatImpl" reference named "chat"
45+
Task {
46+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
47+
let result = try await channelGroup.listChannels()
48+
49+
for channel in result.channels {
50+
debugPrint("Channel: \(channel.id)")
51+
}
52+
}
53+
// snippet.end
54+
}
55+
56+
// MARK: - Add Channels
57+
58+
func addChannelsToGroup() {
59+
// snippet.channelGroups.addChannels
60+
// Assumes a "ChatImpl" reference named "chat"
61+
Task {
62+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
63+
let supportChannel = try await chat.getChannel(channelId: "support-channel")
64+
let generalChannel = try await chat.getChannel(channelId: "general-channel")
65+
66+
if let supportChannel, let generalChannel {
67+
try await channelGroup.add(channels: [supportChannel, generalChannel])
68+
debugPrint("Channels added successfully")
69+
}
70+
}
71+
// snippet.end
72+
}
73+
74+
// MARK: - Add Channel Identifiers
75+
76+
func addChannelIdentifiersToGroup() {
77+
// snippet.channelGroups.addChannelIdentifiers
78+
// Assumes a "ChatImpl" reference named "chat"
79+
Task {
80+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
81+
try await channelGroup.addChannelIdentifiers(["support-channel", "general-channel"])
82+
debugPrint("Channel identifiers added successfully")
83+
}
84+
// snippet.end
85+
}
86+
87+
// MARK: - Remove Channels
88+
89+
func removeChannelsFromGroup() {
90+
// snippet.channelGroups.removeChannels
91+
// Assumes a "ChatImpl" reference named "chat"
92+
// Assumes "ChannelImpl" references named "supportChannel" and "generalChannel"
93+
Task {
94+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
95+
try await channelGroup.remove(channels: [supportChannel, generalChannel])
96+
debugPrint("Channels removed successfully")
97+
}
98+
// snippet.end
99+
}
100+
101+
// MARK: - Remove Channel Identifiers
102+
103+
func removeChannelIdentifiersFromGroup() {
104+
// snippet.channelGroups.removeChannelIdentifiers
105+
// Assumes a "ChatImpl" reference named "chat"
106+
Task {
107+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
108+
try await channelGroup.removeChannelIdentifiers(["support-channel", "general-channel"])
109+
debugPrint("Channel identifiers removed successfully")
110+
}
111+
// snippet.end
112+
}
113+
114+
// MARK: - Watch Channel Group (Connect)
115+
116+
func watchChannelGroupAsyncStream() {
117+
// snippet.channelGroups.connect.asyncStream
118+
// Assumes a "ChatImpl" reference named "chat"
119+
Task {
120+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
121+
122+
for await message in channelGroup.connect() {
123+
debugPrint("Received message on channel \(message.channelId): \(message.text)")
124+
}
125+
}
126+
// snippet.end
127+
}
128+
129+
func watchChannelGroupClosure() {
130+
// snippet.channelGroups.connect.closure
131+
// Assumes a "ChannelGroupImpl" reference named "channelGroup"
132+
133+
// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
134+
// to receive updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
135+
// and no further items will be produced. You can also stop receiving messages manually
136+
// by calling the "close()" method on the "AutoCloseable" object.
137+
autoCloseable = channelGroup.connect { message in
138+
debugPrint("Received message on channel \(message.channelId): \(message.text)")
139+
}
140+
// snippet.end
141+
}
142+
143+
// MARK: - Get Present Users
144+
145+
func getPresentUsersInGroup() {
146+
// snippet.channelGroups.whoIsPresent
147+
// Assumes a "ChatImpl" reference named "chat"
148+
Task {
149+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
150+
let presenceByChannel = try await channelGroup.whoIsPresent()
151+
152+
for (channelId, userIds) in presenceByChannel {
153+
debugPrint("Channel \(channelId) has users: \(userIds)")
154+
}
155+
}
156+
// snippet.end
157+
}
158+
159+
// MARK: - Stream Presence
160+
161+
func streamPresenceAsyncStream() {
162+
// snippet.channelGroups.streamPresence.asyncStream
163+
// Assumes a "ChatImpl" reference named "chat"
164+
Task {
165+
let channelGroup = chat.getChannelGroup(id: "my-channel-group")
166+
167+
for await presenceByChannel in channelGroup.streamPresence() {
168+
for (channelId, userIds) in presenceByChannel {
169+
debugPrint("Channel \(channelId) now has users: \(userIds)")
170+
}
171+
}
172+
}
173+
// snippet.end
174+
}
175+
176+
func streamPresenceClosure() {
177+
// snippet.channelGroups.streamPresence.closure
178+
// Assumes a "ChannelGroupImpl" reference named "channelGroup"
179+
180+
// Important: Keep a strong reference to the returned "AutoCloseable" object as long as you want
181+
// to receive updates. If the "AutoCloseable" is deallocated, the stream will be cancelled,
182+
// and no further items will be produced. You can also stop receiving presence updates manually
183+
// by calling the "close()" method on the "AutoCloseable" object.
184+
autoCloseable = channelGroup.streamPresence { presenceByChannel in
185+
for (channelId, userIds) in presenceByChannel {
186+
debugPrint("Channel \(channelId) now has users: \(userIds)")
187+
}
188+
}
189+
// snippet.end
190+
}

0 commit comments

Comments
 (0)