在Swift中保持套接字打开以进行聊天应用

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.

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".

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.