Detect WebSocket connection closed - android

I'm using node.js and WebSocket-Node to build a simple chat websocket server.
The client is an Android app.
The server have to communicate with the clients in two way:
websocket message when the client is connected (app open in foreground)
Push notification using Google GCM else
But there is a little problem that i need to figure out..
When the client is connected to the server and the internet connection drops, the server is not notified about that because the client did not send the closing frame.. So for like 10 seconds, the server thinks that the connection is still alive.
If in those 10 seconds the server send the message over the socket.. the message can never reach the client.
I need to detect when the connection is closed, or another way to send the message using GCM when the connection it's closed.
Is there a way?

From the side of the server an unclean disconnect (no disconnect messages sent) is indistinguishable from a very slow connection.
What you need to do is have some sort of heartbeat. WebSockets has pings on the protocol level for this. If your library exposes this, then set the ping to a delay you are willing to accept, and add a handler for when this fails/doesn't complete after a timeout.

Answering my own question, because: It is not possibile, and I have found another better way to handle my specific case.
When the server send a message to the client, the client reply to the server, to let the server know that the message has been delivered to the client.
If the server do not receive the response from the client after a timeout, the connection is considered closed.
Proceed to send the message over GCM (or store it to be sent again when the client reconnect).

Related

From server how to call an Android app without any request?

In my app I have functionality like server-side some updates is done. When every updates is happen in server-side I need send those data to app without using push notifications.
Take example of Ola Cabs. Whenever user open the app it will show all cabs information nearby user. At the same time it will send data to the cab drivers telling that a customer needs cab like that. How to implement this functionality with out push notifications?
By using this things we can do that
1) push notifications
2) explicitly calling the server from client with given time (its bad process)
Is there any solution other than these things?
How could client get response without request? Please read OSI Model and TCP/IP protocol. Because server cannot send response without request. So client have to initialize request first and wait for response from server.
If you want to do same thing without GCM then i will suggest you to use socket programming. In that you can be open client port continuously to receive messages (response) without any request initiated by client.
But again, continuously opening socket port, drains device batteries, and this is bad process. In case of GCM it only open port for while when client send request to server and close after response. So I suggest you to use GCM but still you have problem with GCM then use socket programming like chat applications (e.g WhatsApp)

GCM XMPP delivery receipt send other machine and GCM closes connection when receive its ack

I have an application running on two machines, each of them are have established a connection through GCM via TCP(XMPP).
One issue i'm experiencing is that sending an xmpp message through one connection with the 'delivery_receipt_requested' flag, does not necessarily means receiving the receipt for that message on the same connection. This is obviously very problematic, since connections might be set up on different processes, sometimes on different machines.
An other issue is that machine received delivery_receipt (which does not send original message) tries to send a delivery receipt ack to the GCM, GCM closes this connection.
Does it seem to be weird? GCM send delivery receipt an other machines and when it receives the delivery receipt ack from that machine it closes the connection since message does not send from that machine?
I have found the reason why connection is closed. I had many connection and my application sent deliverty_receipt_ack to the GCM over any connection in connection pool. And after some period of time google sent XMPP error with "received more ack than expected" then connection was closed.
This communication model causes the connection-closed problem, correct way is to use the connection received deliverty_receipt in order to send delivery_receipt_ack, in other word using same connection to send delivery_receipt_ack in reponse to delivery_receipt solves the problem.
is it a must to send an ack for delivery receipt? i haven't any issues so far without sending it, well except for the huge delay when the phones have lousy connections, e.g. from a foreign country.

android Handle multiple clients connected to a server using sockets

I have an Android app that needs to let multiple socket communication. here is the basic work around:
My application( Say Server) is a service which will run as a sever socket. other application can connect to it on predefined port. There can be a multiple connection say client 1, client 2.
So when client 1 send hello, server will process this and should send reply back to client 1.
Currently I am able to connect server for client 1 and client 2. I have created input , output buffer for same. But I am not sure how server will detect from which it has received "hello".
I was referring this example , but still not clear how to use that solution:
How can I handle multiple clients connected to a server using sockets?
The ServerSocket.accept() call, which creates a connection on the server side, returns a socket. Each call to accept will create a new Socket for communication with that particular client. So whatever Socket sends you input is the Socket that needs a response.
First I tried to check thread ID whenever A new connection is established, but it was same as it is Server Id.
Whenever the request received from a specific thread it has a ID. you will get it by
Thread.currentThread().getId();
This is unique for each client. So I save this and use this to detect to which thread I should reply back.
Thanks all for your support.

Android client tell Syn & receive Rst Ack

I'm working for an App Android and I need my phone was always connected to my server MQTT...
But my phone can lost his connection(Tel :[FIN;ACK]; Serv:[FIN;ACK];Tel:[ACK]); after that, when he send a [SYN] message for connect it to the server MQTT, the serveur send an [RST,ACK]... And this while the service of my app is in activity.
In my case , i MUST to re-launch my server if i want my phone can be reconnect, but, i can't reboot my server (multi-phone possible, and i need real-time performence).
There is there a possibility to connect my phone without reboot my server?
Thank's for time.
Best regard
Guillaume
I think your problem is similar to the one I had. The connection was half closed. See http://www.codeproject.com/Articles/37490/Detection-of-Half-Open-Dropped-TCP-IP-Socket-Conne for details.
The way I solved this was to i) add keep alive msgs, every 5 secs. from the phone to the server and ii) have the server close the connection if there was no incomming data for 15sec. See http://www.forward.com.au/pfod/CheapWifiShield/index.html and www.pfod.com.au
MQTT includes keepAlives (PING request msgs) so you only need to modify your client to specify a short keepAlive interval
see http://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over
to have the MQTT server to more quickly close the connection once it stops getting your data or keepAlive msgs.
Although the above link states
"The broker will close the previous connection to the same client (determined by the same client identifier) and establishes the connection with the newly connected client. This behavior makes sure that half-open connection won’t stand in the way of a new connection establishment of the same client."
So you should not be having this problem at all !!!

Android - How to keep connection with server for a long time

I wrote a chat application for Android using SocketChannel. It connects successfully with the server and all features work. But after a long time since I logged in (about 2-3 hours), I try to send a chat message again and it fails. In log file, SocketChannel, selector still open and connect to server, message already write successful. What's the problem? What am I missing?
Thanks in advance for your help.
I have had a same kind of problem with SocketChannel. I have used SocketChannel based library to connect to a websocket from andoird application (The library is Autobahn websocket). I can successfully connect to web socket and can send/receives messages through the websocket. But After sometime(may be after 30 mins, 1 or 2 hours) the the websocket connection hangs. As your scenario, logs seems socket connection is still open and connect to server, message already write successful. But server won't receives the messages.
In my scenario problme is not with the SocketChannel. Problem is with TCP time out. In order to keep the connection alive I'm sending peroidic ping message from server to client. Web socket default support this messages call PING/PONG messages. This ping message need to be send to clients in periodic time, in order to keep the connection alive. When ping message receives client automatically replies PONG message to server. By using this approach I was able to keep the socket connection for a long time.
More information about this approach
Solution for websocket hanging, Websockets. Loss of internet, keep-alive messages, app architecture etc, websocket protocol and ping/pong messages
Hope this would help to you(even though you are not using websocket). The approach could be same in websocket as well as your chat application.
if you want to create a Chat for android or something else with push from a server, try it with MQTT
http://tokudu.com/2010/how-to-implement-push-notifications-for-android/
its is a very cool thing, just take a look at it :-)

Categories

Resources