0

I am developing a chat client using Socket.io as a means of communicating with the server. As such, using GCM is not exactly an option.

I want be able to receive chats at any time (even if the application is closed); essentially the socket needs to be listening at all times. I understand that a service can be started when the phone boots and kept running. However, this seems to be a rather bad idea on a mobile device, draining battery and the like.

Researching, it seems that GCM is the most common implementation for this sort of issue. Unfortunately, I cannot replace Socket.io at the moment. I was wondering how I would go about implementing this chat design - having a socket listening in the background.

DaPasta
  • 11
  • 1
  • 4

2 Answers2

1

You should run one service(e.g. ListenerService) when your application start and inside that service you must start one thread(e.g. ListenerThread). And keep that thread and service alives till your application is in onResume()/onPause(). Through that thread connect your client socket to server. and receive all updates on ListenerThread. and if you want send some packets to server then send on another thread/handler(e.g tempThread) and close that thrad after sending request.
When your application close/exit then stop that service(e.g ListenerService). And In that service you must sleep ListenerThread. Do not try to stop thread for more information about thread you can follow this link and this link. Thread.stop() is deprecated in android from API level 1, please see document. Start service(ListenerService) with START_STICKY. for reference please refer android documention of service

Community
  • 1
  • 1
Mangesh Sambare
  • 594
  • 3
  • 23
  • 1
    Thanks for the answer. However, what I am looking for is a way to have the socket listening at all times, even if the application is closed. So if someone sends a chat message to the user, the user can be notified even if the user is not directly using the application. From what I see, your solution does not account for this. – DaPasta Dec 22 '15 at 07:59
  • if you want to get chat messages even if application closed then do not stop service(ListenerService) and also do not stop thread. – Mangesh Sambare Dec 22 '15 at 09:28
  • just keep it running in background like other applications (WhatsApp, Gmail etc.). But while running thread or opening port of socket continuously drain device battery. – Mangesh Sambare Dec 22 '15 at 09:35
  • Thanks, I'll try it out. – DaPasta Dec 22 '15 at 23:23
  • For battery draining issue you can try intelligent way like close socket while device is in sleep mode and reconnect it again when device is in on state. I thought chatting application doing same thing that's why user get messages after device in wake up state. But also it depends on device what settings configured by user for particular device. In android devices their are lots of things can user do it or configure it like while in device sleep mode do not disturb functionality. Please try it this also and Let me know. – Mangesh Sambare Dec 23 '15 at 04:41
  • Hello @Sam, I tried to follow your answer and just wondering on how to handle socket events on the service (or should socket events remain in Activity)? I'm kind of confused... Thanks a lot. http://stackoverflow.com/questions/40603404/how-to-handle-socket-events-as-background-service-in-android – Woppi Nov 15 '16 at 06:38
0

GCM is the correct way to handle this. Send the GCM alert to the device and then the user can click it to open your app. At that time you can re-open your socket and get the data needed.

If you ever plan to deploy your app to iOS this is the route you will be taking there. iOS kills your background tasks after 5 minutes or less if it feels like it.

greg_diesel
  • 2,955
  • 1
  • 15
  • 24