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
asp.net-core
urbancode
c#-4.0
dictionary
google-play
appx
cplex
routes
spring-jdbc
jpeg
survey
angular-ui-bootstrap
constraint-programming
contact
maude-system
numeral.js
n-gram
commonmark
hapi
dynamics-crm-online
dosgi
transformation
alpine
graphicsmagick
language-agnostic
icloud-api
java-7
javacv
publish
delicious-api
bpel
http-status-code-503
jspresso
caret
mpmediaquery
overriding
code-search-engine
lumberjack
avro4s
core-plot
angular2-meteor
angular-resource
fault
recursive-datastructures
drupal-6
kbuild
nsarray
r-forge
dwscript
sonarlint-vs
crosswalk-runtime
rotativa
azure-virtual-network
fody
firebaseui
clang-static-analyzer
ubuntu-10.04
superstack
php-parse-error
photobucket
maven-tomcat-plugin
teamcity-8.0
endeca-workbench
ruby-2.2
browser-bugs
sframe
microbenchmark
interrupted-exception
mono-embedding
clipperlib
pick
codeigniter-url
coldbox
asp.net-web-api-odata
navigationservice
stxxl
plasma
enterprisedb
nsnetservice
armcc
uiviewanimation-curve
ohm
semantic-diff
punbb
data-loss
appender
lang
subviews
icanhaz.js
user-friendly
web-architecture
zend-tool
yagni
phonon

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App