Sending message from server to client without managing sockets - iphone

Im building a chat app for iPhone and im using NSURLConnection with HTTP Post and Get to send messages from client to server. From what i understand this is a non-persist connection, because NSURLConnection open and close connection for each HTTP request, am i right??
In my server i just response to this messages.
My main problem is how to send a message from client A to client B?
client A sends the message to the server with HTTP POST, but how can i send the message from the server to client B if im not keeping a persists connection to client B?

You could make use of push notifications to inform the client that it has an unread message.

Related

If I send some data then immediately close the socket, will the other end receive the data?

For .NET framework System.Net.Sockets.Socket, if I send some data then immediately close the socket, will the other end receive the data?
If not, is there a way for me to close the socket only after the other end receives the data?
WebSocket over HTTP works by establishing a TCP/IP connection, which requires the clients to sends an upgrade header in the payload to the server, and if the server can establish the create a WebSocket connection, it will reply with a HTTP 101 Switching Protocols response. Once the connection is established successfully, you can send data back and forth.
Interesting enough, requesting to close the connection also requires you to send a frame, which means you are also sending data to the server. Not even that, to properly close the connection, the sever also needs to respond with the same closing frame, once that happens the TCP connection is terminated and you can no longer send data.
What that means in your case is the server would receive the data you had sent before sending the close request frame.
You can read more about the details of frame, http headers, etc in the protocol RFC https://tools.ietf.org/html/rfc6455

Ejabberd Message delivery while application is terminated

I am developing a chat app using ejabberd server for both IOS and Android. I also wrote a module for ejabberd to get the offline messages sent to my own server api .
my own server api will send notifications to the IOS/Android platforms using FCM.
On the client side , if the application is in the foreground or the background , it will stay connected to ejabberd and if the client receives the message then ejabberd will send the message delivery status.
I am facing an issue while the app is terminated ( service is not running ) which means it is not connected to ejabberd (offline) . if i send a message to this app while it is not terminated , it will receive a notification but the message still undelivered . how can mark the messages as delivered when receiving the notification while the app is terminated.
to explain it more , the same functionality is working fine with whatsapp :
device A has whatsapp installed and whatsapp was turned off (terminated)
Device B has whatsapp running
Device B sends a message to device A
Device A receives a whatsapp notification
Without doing anything on Device A , the message status on Device B is marked as delivered .
How can I implement this scenario with ejabberd ?
In case someone went into this issue , here is the solution that I implemented with help of #Mickaël Rémond from his answer.
I configured ejabberd to send the offline messages to an http service ( your own server) please refer to this link for further on how to do it
your server should catch the above call and generate a notification message (FCM ) in my case and send it to recipient device
recipient device will catch the notification which includes the message
recipient device will call http service (your own server backend)that responsible for sending the deliver ack to the original sender . you need to pass from, to , stanzaId , vhost with this call
backend server will use ejabberd-api (set of exposed apis to manage ejabberd through rest apis calls) to send delivery message using this api
please note the following notes also :
sending the delivery message from your own server to ejabberd will not delete them from ejabberd database
if the user re-connected to the ejabberd server then the recipient will receive the message again from ejabberd .
It is probably too complex for a simple Stack Overflow question, as you need to integrate several moving part on client and server:
You need to execute code in background when receiving push notifications on iOS (you need that property set on your app in your app provisioning profile and have code to handle that). The client will initiate an HTTPS query to let the server know that the message was delivered.
You need to have an endpoint that will get the delivered HTTPS calls and generate either a message ack or a chat marker on behalf of the user and route it in ejabberd.
In real world, this is not enough if you want to take into account the fact that you can only have 1 push in the queue on APNS. If you have several messages sent while the device is not on the network, you will need to have the device check all received messages while offline on the server, otherwise you will lose messaging.
You need to rely on XMPP Message Archive Management (MAM) to handle that history.
As you see, this is not a simple few tens of line of codes but need real design and involved work.

How can a Bayeux Client stop receiving messages from Bayeux Server? Is there a way to block the channel between client and server at the client side?

At the server, CometD provides a MaxQueueListener hook to drop messages but if the Bayeux Client wants to stop receiving messages from a server without disconnecting , can it achieve that?
A BayeuxClient receives message from the server only for the channels it is subscribed to.
For a BayeuxClient to stop receiving messages from the server is enough to unsubscribe from all channels it subscribed to.
The BayeuxClient will still receive meta messages on meta channels that are part of the Bayeux protocol, but no application message will be delivered by the server.

Using XMPP for Request Response

I would like to know if is it possible to get XMPP messages sent by mobile client applications in my PHP Back end server?
The PHP Back end server will process the messages (JSON request) and send responses back to the client mobile apps. There will be 5 million client application users sending messages to server simultaneously.
Well you can use XMPP but definitely for that, you will need alot of changes.
Your mobile clients will send data in XMPP packets (message or iq) and you will have to handle those stanzas and parse them to get the data (which can be JSON).
So, Yes you can use XMPP but you need to have such skills (both on client side and on server side).

Socket to receive data and push to client browser in a web application

There is a SocketServer which I need to connect, receive data and then push it back to clients (web browser).
I just need to confirm the approach. I will create a Socket connection which will keep listening for message inside a ServletContextListener. The message will be then be stored in ServletContext so that it's available to push it back to client browser.
Should I create socket connection inside ServletContextLister? Or is there any other way to listen to socket?
For pushing data back to client, I don't want to have a bi-directional communication. So should I go for SSE or Html5 WebSockets? Any java framework supporting SSE or WebSocket along with client side library?
Thanks
I implemented this and its working fine. For server I went with Jetty 8 and used jetty's WebSocket.
Briefly how I implemented is, for listening to messages from SocketServer I implemented ServletContextListener which initializes Socket which will consume the messages.
When a user hits a url I add the WebSocket connection to a Collection and store that collection as ServletContext attribute. This WebSocket list is then accessed by the Socket which iterates and sends messages where whenever messages arrives. The message is then pushed back to all the connected clients.