long-polling


Koa 2 router how to make a long poll request


I want to make a long pooling request. So I want to take a request and send a response after some delay. Is it possible?
I'm trying to use the async/await synax but it's isn't working for me (I got an error 404 on the client)
many thanks for any help.
Here is my server
import 'babel-polyfill';
import Koa from 'koa';
import Router from "koa-router";
import fs from "fs";
const router = new Router();
const convert = require('koa-convert')
const serve = require("koa-static");
const app = new Koa();
router
.get('/*', async function (ctx, next) {
ctx.response.type = 'text/html; charset=utf-8';
/* await (() => {
setTimeout( () => {ctx.body = fs.readFileSync(__dirname + "/public/index.html")}, 1000)
})(); */
//ctx.body = fs.readFileSync(__dirname + "/public/index.html");
})
app.use(convert(serve(`${__dirname}/public`)))
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
Generally, yes. This is possible.
The problem in your code is, that await is entierly based on promises. So your timeout function needs to be encapsulated into a promise. Something like this could work:
...
function delayed(ctx, ms) {
return new Promise((resolve, reject) => {
setTimeout(function() {
ctx.body = fs.readFileSync(__dirname + "/public/index.html")
resolve();
}, ms);
})
}
router.get('/*', async function (ctx, next) {
ctx.response.type = 'text/html; charset=utf-8';
await delayed(ctx, 1000);
})
...

Related Links

Koa 2 router how to make a long poll request
Confluent long polling properties in kafka-rest
How to combine async and blocking requests in VuGen?
Long polling confusion
WAS Liberty & Atmosphere long-polling 1 min delay
Long Polling interval in atmosphere
MQTT long polling over GPRS/HSDPA
JS and CSS file fails to load when the page is refreshed in grails application which uses Atmosphere Meteor plugin
Security concerns in long polling
Kafka long polling
How to build the request for a long poll url for box.com
how to access enterprise events using Java code
Handle client timeouts when serving long polling with gevent
Long polling blocking multiple windows?
Long polling on a penny auction site?
Cannot implement long polling

Categories

HOME
java
magnific-popup
google-api-php-client
comparison
smarty
platform-builder
grep
sqlite-net-extensions
spring-jdbc
installshield
serverspec
podio
webrequest
esper
synchronization
applepay
offline
wheelnav.js
imacros
quickbooks
qt-creator
vb.net-2010
libtiff
google-static-maps
django-cms
reverse-proxy
language-agnostic
firebase-crash-reporting
android-widget
fopen
maquette
neo4j-spatial
c++-amp
google-rich-snippets
key-value-observing
restlet
sql-server-agent
janrain
android-mediaprojection
gpx
convertapi
avconv
chain-builder
typescript1.8
git-diff
nstextview
setter
forever
sqlclient
typed-lambda-calculus
google-perftools
python-cryptography
django-scheduler
or-tools
ableton-live
google-feed-api
intrusion-detection
django-debug-toolbar
persist
moveit
pickadate
srand
lemon
appfabric-cache
vstest.console.exe
oberon
client-side-templating
picturefill
fmod
gnu-smalltalk
mysqltuner
runtime.exec
dotnetnuke-5
e4x
parametric-equations
punbb
subscript
fireworks
gcj
psi
rfc1123
calling-convention
actionview
lang
wise
asp.net-profiles
backcolor
yagni
spec#

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile