Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
topriddy
Posts: 9
Registered: ‎06-16-2010
My Device: Bold 9700
Accepted Solution

Keeping Socket Connections Alive

Hello Developers,

 

Please I am having problems keeping a socket connection in my application alive for a long period. It seems to disconnect after a short while (every 2 minutes) especially if idle.

 

It happens that I am writing an application with a Chat feature and this requires I keep a socket connection Alive for the life-tme of my application. I have already tried tricks like pinging server every minute, but then this constitute a drain of (Blackberry) resources. Blackberry Push isnt an option (as i have other clients other than BB devices).

 

Please does anyone know how i can solve this or still suggest another alternative way of going about this?

Developer
Dionysus
Posts: 152
Registered: ‎07-28-2011
My Device: Bold 9900

Re: Keeping Socket Connections Alive

The only way is to have a keep alive request sent to your server every minute or so ... BlackBerry kills the socket after about 2 minutes of inactivity (idle).

New Contributor
topriddy
Posts: 9
Registered: ‎06-16-2010
My Device: Bold 9700

Re: Keeping Socket Connections Alive

But this uses up considerable blackberry resources i think. I have an App Monitoring app which indicates this. I now even wonder how other chat apps pull this off?

Also, how do i even differentiate between blackberry killing a socket and network killing the socket because of a network loss?

Thank you in advance.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Keeping Socket Connections Alive

I agree with the previos responder. The problem that you have is the reason RIM invented push.  I am not aware of a way round this. If you can't use push, then you are going to have to keep alive and so your app will suffer the battery drain and possibly the additional charges associated with data traffic. 

 

Some people have used email instead of Push as a wau of notifying the application that there is something for it - this requires a email listener in the application to process and remove these notification emails. 

 

If you do use a keep alive message, then I think this quesiton becomes moot:

"how do i even differentiate between blackberry......"

New Contributor
topriddy
Posts: 9
Registered: ‎06-16-2010
My Device: Bold 9700

Re: Keeping Socket Connections Alive

I have accepted as a solution. Though clumsy and NOT what we developers would have wanted by a large margin. I am trying to do some asynchronous communication between server/client and the socket alive is used by xmpp.

 

My question to you though is please do you have any explanation as to why one doesnt have to bother about this in Java SE? Could it be simply because BB Dev is largely different?

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Keeping Socket Connections Alive

java se is not a mobile system, and therefor usually not restricted by battery.
j2me is also a quite old platform.

i saw whatsapp draining the battery quite quickly, so i guess they also use the "keep socket open" approach.
I agree that this is really bad, especially as sending data consumes about 100 times the battery compared to just receiving it, and GPRS_IDLE can never be achieved for power saving.

BB10 does have a completly different architecture, maybe you should consider developing for it instead.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter