Keeping socket open in Swift for chat application - php

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.

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.
Try coming up with a solution for receiving past messages when the client becomes online.
Read about Apple Push Notification Service to notify your users about messages while the app is not active.

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

You need to be in sync with server. Do following steps.
Dump every message in Database i.e. conversation from both parties with timestamp.
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.
When app is killed you can implement APNS from apple.

Related

How to establish a persistent connection to a WebSocket server in Laravel to receive and send messages?

I have created a Laravel backend, which is an API. React frontend interacts with it. The frontend also communicates with the websocket server, receiving messages from it and reacting appropriately from it.
I was given the task of sending messages to a remote web socket from my backend.
I created the defined events to dispatch and listeners for those events. As far as I understand, in listeners I have to send messages to the websocket server. I was trying to use the textalk/websocket library, i.e. creating a websocket connection every time I sent a message to the websocket. It's wrong, but at least it worked. But now I need to keep a permanent connection with this web socket in order to receive messages from it and respond to them. And also I need to still keep sending messages on certain backend events.In general, I need to keep open the connection of my backend with a remote web socket server in order to constantly receive messages from it and in turn send messages to it at certain events within the Laravel application.

Php Notification system and websocket

i'm trying to develop a PHP notification service for a CRM used by my customers.
Scenario: an user add an appointment for a collegue, and this collegue has to receive a notification inside his CRM profile like, for example, Facebook did.
The structure is pretty simple: a PHP Server in listening with the client browser connected with Websockets.
Server: i'm using Ratchet - PHP WebSockets, i've followed the examples and started the server, server that is able now to accept connection and deliver messages to all clients connected or just to one receiver.
Client: a simple JS script, with Websocket connection and method, that handle all receiving messagges and notify all to the user connected.
My point is: i'm guessing to have the websocket client browser just for incoming messagge, i don't want to allow them to send nothing, the send part must be done by the server that has access to DB in this way:
1) FORM Appointment
2) Submit to the server and save it to DB, in this step a client PHP Websocket connect to the Ratchet server and send the notification to the receiver.
3) The Websocket of receiver receive the notification and alarm the customer.
The questions are:
what can i use to do the step 2 with the PHP websocket client to connect with the Websocket server? is good to use Pusher interface like this example suggest: http://socketo.me/docs/push or can i avoid using this system?
how can i handle the idle time of websocket client ? a timer that send a ping like message to client for avoiding the dead of websocket?
-how handle possible down of client side or server side?
THanks to all of you!

iOS - correct way to send push notification to multiple apps from the same server at the same time with the same message and payload

I have two separate applications in "development", lets call them App A and App B , that are supposed to receive the exact same message and payload from the same server as a push notification. Both have different application ids, certificate requests, provisioning profiles, development APN certificates, and produce different tokens.
If I send a push notification to either application individually each application will receive a notification. But if I try to send a push notification to each application concurrently, only the fist application will receive the notification. But I will receive no errors for the other application.
I have tried to delay the push notification by up to 10 seconds between sending the push notification to the last application without success. Is there some way that I need to handle the connections to the apple server to enable this type of push notifications?
Also note that there is another question that is having the exact same issue, but without any answers. Limitations on push notifications for multiple apps from the same server
I was able to get the notifications to be delivered to both of the applications successfully by closing the previous connection. This is not recommended by Apple, and they state that I should keep them open, as there is a potential to have a lot of notifications pushed per day to each application.
Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack
Read More Here
try to use two instance of your server , and keep your connection open while sending.
Connect each instance to a different port: 2195, 2196

J2ME PHP Push Notification

I want to work on a project which has to do with the server sending notification to the client. The client side is a j2me application and the server side script will be implemented using PHP.
I have found out i can use the push registry API in midp 2.0 for push notifications, but i don't know how to implement this such that a socket connection will be created to the server such that notification can be sent to the client via the socket connection. The notification will then trigger the start of the midlet.
How can i use the push API to create a socket connection between client and server and have notifications sent to the client?
Complete answer is here
In short, there are two ways you might want to do it
Launch the application whenever you receive this "Push SMS"
Launch the application just the next time you receive this 'Push SMS"
For situation (1), you will need to specify this in JAD file. If you do this, the application manager will take necessary steps to launch your application the next time SMS is received.
For situation (2), you will need to write code:
PushRegistry.registerConnection("sms://<port number>","Midlet-Class","*");
The above note of mine would address the issue of launching the application upon incoming SMS. After you launch you can connect to your server and post data or retrieve data. This is a practical way of implementing push for J2ME platform.
But you seem to be wanting to launch the application on incoming network connection; this means you are creating a server socket on the phone and server is trying to establish the connection to the phone applications. I did work in such a situation where the phones were deployed on Motorola's iDen network (pre-2005 era). On such networks, it is possible to assign an IP address to the phone which makes the Push upon incoming network connection feasible. But that's past, on none of the GSM/CDMA networks, atleast at consumer's end, this cannot be supported. Hope it answers your question.
You need to do two things, One you need to listen one port number on the J2ME side for the incoming sms. Next you need to attach one GMS Modem to your computer system, Now send sms using this Modem on a particular port to the mobile you are listening. You can use AT Commands to send SMS.
Please visit this Nokia Link.

Apple Feedback Service (APNS) is slow

I have coded Push Notifications (APNS, Apple Push Notification Service) for an iPhone App with PHP. Everything is working fine while no app is deleted from a device. I use the Feedback Service from Apple do get the deleted devices. If I get devices I mark them as INACTIVE in my database to send no further push notification to those devices.
The problem is that Apple is slow. If you delete the app from your device a request is send to Apple and I collect the devices by using the Feedback Service. But Apple is not forwarding the inactivated devices instantly so I get the following error because I can't send push notifications to a inactive device:
PHP Warning: fwrite(): SSL: Broken pipe in apns.php on line 155
My Feedback Service script runs every 5 minutes but the update from Apple comes every 10-15 minutes. If I send a push notification within the 10-15 minutes the above error appears because in the database the device is still marked as ACTIVE and on the Apple server the device is marked as INACTIVE.
There is no problem to connect to the Feedback Service or to the Push Service but I don't always get devices from the Feedback Service.
Greets,
TheFox
Answer from Apple.com Developer Forum (https://devforums.apple.com/thread/114446):
Your server needs to detect disconnections and reconnect if necessary. Nothing is "instant" when networking is involved; there's always some latency and code needs to take that into account.
Also, consider using the enhanced binary interface so you can check the return response and know why the connection was dropped. The connection can also be dropped as a result of TCP keep-alive, which is outside of Apple's control.
So I'm gonna set all devices to INACTIVE if the server detects a disconnection.

Categories