Socket.io: Example wit Koa doesn't work

Created on 11 Jan 2014  路  7Comments  路  Source: socketio/socket.io

Server doesn't serve any data:

var app = require('koa')();
var server = require('http').Server(app.callback());
var io = require('socket.io')(server);

app.use(function *(){
this.body = 'Hello World';
});

server.listen(9000);

Most helpful comment

solved like this:

var app = require( 'koa' )();
app.use(function*(next){ 
   /* your awesome stuff */ 
   yield next;
});
var io = require('socket.io').listen( app.listen(3000) );

i'm still not sure it's the best way though.

All 7 comments

try with io reference io.use(){}

The readme is written for 1.0.

For 0.9.x use:

var app = require( 'koa' )()
  , server = require( 'http' ).Server( app.callback() )
  , io = require( 'socket.io' ).listen( server )
;

i'm also using koa with socket.io 0.9.x:
i've configured the app as @skw suggested but the browser gets a 404 when searching for /socket.io/socket.io.js
any suggestion ?

I have the same problem as @G3z.

Plus if I write _app.listen(80)_ I can go on my pages but _server.listen(80)_ return Not Found.

solved like this:

var app = require( 'koa' )();
app.use(function*(next){ 
   /* your awesome stuff */ 
   yield next;
});
var io = require('socket.io').listen( app.listen(3000) );

i'm still not sure it's the best way though.

I have a Not Found on my browser (with you code or mine).

Edit: Otherwise your solution works.
Edit 2: Ok just my code who probably need an update.
Edit 3: Ok just declare _io_ after my stuff. ^^'

I still got this problem with koa 0.14.0, node 5.1 and socket io v1.3.7 in the Koa example code.
I used G3z's way solved it.

Was this page helpful?
0 / 5 - 0 ratings