Ryan Murphy
Ryan Murphy

Reputation: 163

Keeping socket open in Swift for chat application

I have a working chat server in Php/Ratchet. I am using Starscream as my client in Swift. I successfully created a chat between users however this only works when my application is open because this is when the socket is open. How do I make my app receive messages even if my app isn't on. So basically is there a way to keep sockets open. I read somewhere that it is forbidden by Apple.

Upvotes: 1

Views: 1067

Answers (3)

KPK
KPK

Reputation: 160

You need to be in sync with server. Do following steps.

  1. Dump every message in Database i.e. conversation from both parties with timestamp.

  2. When app goes in to background and comes to foreground flush your all messages which are on your screen/local array for showing messages on screen, call a Sync service which will fetch whole conversation, because definitely server will be having all messages, feed you screen with this service response.

The iOS system will receive socket calls at kernel level but will not pass those messages to you app, so you will not get messages while app is in background.

  1. When app is killed you can implement APNS from apple.

Upvotes: 0

danday74
danday74

Reputation: 56996

Sounds like you are sending messages from your client to your phone. You need to send messages to a server and the server sends them to your phone.

The server, if unable to successfully send a message to your phone, should store the message and should send the stored messages to the phone when the phone is available.

There are message queuing systems for things like this. See, for example, RabbitMQ. There are loads of others too that may work better with your tech stack. Do some research.

Here the client talks to the server, the server hooks up with RabbitMQ or whatever you choose to use, RabbitMQ keeps a track of queued messages and when the phone comes online, RabbitMQ sends the queued messages to the server and the server sends them to the phone.

PS Google "message queue PHP".

Upvotes: 1

Misha Karpenko
Misha Karpenko

Reputation: 2186

There's a high probability that your process is going to be shutdown by the system at some point, so I wouldn't rely on the app being active in the background.

  1. Try coming up with a solution for receiving past messages when the client becomes online.
  2. Read about Apple Push Notification Service to notify your users about messages while the app is not active.

Upvotes: 2

Related Questions