Work on background with sync (#131)
Open
Unknown opened 2 years ago

Hi, I need to create a method that reads messages synchronously and all the logic needs to stay within this method. I looked for a solution on documents and blogs but with no luck, hope this is the right place.

For login I use loginAndWait, but for messages I have to use EventHandler. Is there a synchronous method in which I can receive the MessageModules?

Can you give us a sample or an idea on how to do it? something like:

func readAllMessageSync(){
	let client = XMPPClient()
	client.modulesManager.register(AuthModule())
	client.modulesManager.register(StreamFeaturesModule())
	client.modulesManager.register(SaslModule())
	client.modulesManager.register(ResourceBinderModule())
	client.modulesManager.register(SessionEstablishmentModule())
	client.modulesManager.register(PresenceModule())
	client.modulesManager.register(MessageModule())

	client.connectionConfiguration.userJid = BareJID("jidSamle")
	client.connectionConfiguration.credentials = .password(password: "jidSamlePass", authenticationName: nil, cache: nil)

	try await client.loginAndWait()
	
	let presenceModule: PresenceModule = client.modulesManager.getModule(PresenceModule.ID)!	
	presenceModule.setPresence(show: Presence.Show.dnd, status: "Do not distrub me!", priority: 2)
	
	try await readIncomingMessage(client)
}

func readIncomingMessage(client: XMPPClient){
	//exit if 0 message else get message as MessageModule.MessageReceivedEvent
}
        

thank you Luigi

Unknown commented 2 years ago

Please do not use EventHandler - it is deprecated. Use Combine framework and subscribe for events in MessageModule::messagesPublisher.

There is no way if or how many messages are waiting for you with your approach, so it is impossible to implement method which you try to implement. Moreover, messages in XMPP are sent to the client when sender sends them - so you may receive a message while you are fetching them.

For the "fetch messages since" feature, please consider using MessageArchivingManagementModule to fetch messages from history (since timestamp or since message id). This will publish proper events in MessageArchivingManagementModule (combine-based), and queryItems() method will return when it will fetch all messages form this batch. (In the meanwhile you would collect all messages published by MessageArchivingManagementModule).

Unknown commented 2 years ago

thanks @hantu85, this unhealthy idea came to us because we are thinking about how to update the mobile app when a push arrives.

The basic idea is to wait for a push notification to trigger an app update to be ready to open. The pushes must be processed in a short time, to correctly display the title and the data inside we must have already read the data in incomimg. And obviously if a user click on a push notification the app must be ready to open. can we handle this case?

Unknown commented 2 years ago

@hantu85 Can you give us some guidelines or sample on how to update the client when a push notification arrives? Thanks

issue 1 of 1
Issue Votes (0)
Watchers (0)
Reference
tigase/_libraries/Martin#131
Please wait...
Page is in error, reload to recover