I have an API endpoint which performs an action based on a user's request, and then echoes a json response. This endpoint also needs to trigger a notification, but only after a 30 second delay. The reason is in case the user reverses their request within that delay (via another endpoint), which would would mean no notification should be sent. Here's the relevant piece of code:
// response
echo json_encode(
array(
'success' => 1,
'updateUser' => $updateUser['ok'],
'updateTarget' => $updateTarget['ok'],
'updateActivity' => $updateActivity['ok']
)
);
// send push notification after delay
sendDelayedNotification($user_id, $target_id);
The sendDelayedNotification
function has sleep(30)
in it to delay its execution. The function then checks if the condition still exists that warrants a notification.
The issue is that even though this function fires after the echo
statement, it delays the response and the request times out. Is there a way to code this so that the response isn't delayed?