Comments (12)
There are some attempts in comments: https://www.php.net/manual/en/features.connection-handling.php
For such long-term running cases, I usually render ordinary response and from browser then, invoke WebSockets/Ajax request with ignore_user_abort()
on server side.
from http.
You mean fastcgi_finish_request
?
from http.
@JanTvrdik Maybe yes, but this function can be sometimes unavailable:
from http.
Then I don't understand this issue. You cannot implement this in userland.
from http.
ping @janbarasek
from http.
@dg I'm not sure this is Nette's responsibility, but it would be nice to be able to control all the output to the browser via the Http\Response
object.
For example, a user invokes a long-running task in the Presenter, so I send him a quick response (Latte template), but the task continues to run in the background even after the user closes the connection and all headers are sent correctly.
This allows, for example, to respond to a request very quickly, but to write data to the database only after the user does not have to wait for it.
If this does not make sense, please close the issue. Thanks.
from http.
The question is, how to implement it? Is there a function for that?
from http.
Before any output is sent
ignore_user_abort(true);
When connection should be closed, output sent and script continue:
if (connection_aborted() === 0) {
ob_flush();
flush();
if (session_status() === PHP_SESSION_ACTIVE) {
session_write_close();
}
}
It should do the trick, but be beware of possible issues:
- IIS + fastcgi doesn't work at all https://bugs.php.net/bug.php?id=60586#1378935714
- it should be checked whether session is written into or output is sent after connection was closed
- I found a note that current working directory is changed after connection is closed, but never really tested it.
- Time limit is a must-have, in case of
ini_get('max_execution_time') === '0'
would be an exception thrown on connection abort welcome
@milo Good job, you was faster :D
from http.
But ignoring the implementation question - there are imho better options for non-blocking processing like message queues, swoole and possibly fibers in the near future.
I am kinda worried about constantly running into race conditions because this behavior is global and e.g. depending on Doctrine EntityManager->flush() auto-called on script termination is quite often. Whether sql transaction or redirect finished first could create quite unexpected behavior, at least for less advanced users
from http.
Wouldn't it be better to write a feature request on PHP?
from http.
@mabar I think the magic is only in Content-Length header.
from http.
Cannot be solved in PHP.
from http.
Related Issues (20)
- Multiple GET parameters of same name dropped from parsed query HOT 1
- Cookie nette-samesite HOT 6
- Type error in FileUpload->getImageSize()
- Unable to extend UrlScript HOT 2
- Creating array property in SessionSection has no effect HOT 1
- Feature request: fight against Google FLoC
- IRequest::setCookie does mot match Request::setCookie HOT 1
- UrlImmutable should implement static get() method HOT 5
- Session id handler HOT 4
- Allow hostname in http > proxy
- Allow disabling same-site cookie
- Invalid session configuration option 'readAndClose'
- Notices are ignored on session_start() HOT 4
- SessionSection::setExpiration checks session's expiration, but session may not be started yet -> warning HOT 2
- IRequest::getFile does mot match Request::getFile
- If website has nginx restricted access by `auth_basic_user_file` $httpRequest->getUrl()->getAbsoluteUrl() returns path with auth parameters included HOT 1
- Call request->getRemoteHost() can cause to stuck app HOT 2
- Unfortunate crossover of `HTTP_HOST` and `SERVER_PORT` variables HOT 3
- Htmlspecialchars for this? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http.