I have created 4 messaging channels and my app loops through and tries to join each one on startup. I wait 2 seconds and test each to verify that it’s been joined, but only 1 of 4 is successful. I don’t receive any errors. The 1 channel that is successful is random…could be any one of the four. Here is the code I’m using to test:
dStore.find(queryBuilder: qBuilder) { threads in //there are only 4 threads
for t in threads {
let t = t as! MsgThread
self.msgThreads.append(t)
//subscribe to channel
let channel = Backendless.shared.messaging.subscribe(channelName: t.objectId!) //channel name is the objectId of the message thread
channel.join()
Task.init {
try await Task.sleep(for: .seconds(2))
if channel.isJoined { print("\(t.objectId!) channel joined... ")} else { print("\(t.objectId!) channel NOT joined..") }
}
let _ = channel.addStringMessageListener(responseHandler: { message in
var msg = Msg()
msg.msgTxt = message + "from cloud"
t.messages?.insert(msg, at: 0)
print("Received a message: \(message)")
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
}
} errorHandler: { fault in
print("Error: \(fault.message ?? "")")
}
Correct…each t.objectId is different. Here is what the log shows…
F4E51784-E12F-414A-967D-2BD9EF7016EC channel joined…
F5DD6F5D-6575-42A3-8DFB-ACC6526F34B channel NOT joined…
F37FA5FA-F82D-43B0-A39D-860A522056 channel NOT joined…
F61B7956-00B4-4520-985B-E42610491F57 channel NOT joined…
and if i run it again…
F4E51784-E12F-414A-967D-2BD9EF7016EC channel NOT joined…
F5DD6F5D-6575-42A3-8DFB-ACC6526F3E4B channel joined…
F61B7956-00B4-4520-985B-E42610491F57 channel NOT joined…
F37FA5FA-F82D-43B0-A39D-86E0A5220E56 channel NOT joined…
Also worth noting that the other channels never gets joined. For example, if i wait a few minutes and push a msg to the channel from the web interface none of the other channels have connected so I don’t think its a timing issue. It seems to fail when it tries to connect, but I’m not getting any errors.
also interesting is that if I try to subscribe each thread to the default channel the same thing happens…only 1 of the 4 attempts work. So the issue isn’t channel dependent. Here is the code for that:
threadStore.find(queryBuilder: qBuilder) { threads in
for thread in threads {
let t = thread as! MsgThread
self.msgThreads.append(t)
//subscribe to channel
t.channel = Backendless.shared.messaging.subscribe()
let _ = t.channel.addStringMessageListener(responseHandler: { message in
var msg = Msg()
msg.msgTxt = message + "from cloud"
t.messages?.insert(msg, at: 0)
print("Received a message: \(message)")
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
Task.init {
try await Task.sleep(for: .seconds(5))
if t.channel.isJoined { print("\(t.objectId!) channel joined... ")} else { print("\(t.objectId!) channel NOT joined..") }
}
}
} errorHandler: { fault in
print("Error: \(fault.message ?? "")")
}