Skip to content
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

Work on background with sync #19

Open
LuigiBertoneri opened this issue Sep 21, 2022 · 3 comments
Open

Work on background with sync #19

LuigiBertoneri opened this issue Sep 21, 2022 · 3 comments

Comments

@LuigiBertoneri
Copy link

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

@hantu85
Copy link
Contributor

hantu85 commented Sep 21, 2022

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).

@LuigiBertoneri
Copy link
Author

LuigiBertoneri commented Sep 21, 2022

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?

@LuigiBertoneri
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants