Skip to content

Commit

Permalink
Merge pull request #13 from laggingreflex/attach
Browse files Browse the repository at this point in the history
use existing app.server
  • Loading branch information
mattstyles committed Feb 8, 2016
2 parents 7627184 + 51b77af commit 038d72e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ app.server.listen( process.env.PORT || 3000 )

## Attaching to existing projects

The `attach` function is used to attach the `IO` instance to the application, this adds `server` and `io` properties to the koa application and should happen before the app starts listening on a port.
The `attach` function is used to attach the `IO` instance to the application, this adds `server`\* and `io` properties to the koa application and should happen before the app starts listening on a port. \*If `app.server` already exists (as an http server), it uses that instead.

The only change you need to make to your existing code is to start the server listening by calling `app.server.listen` rather than `app.listen` (you’ll get a console warning if you get it wrong ;) ).

Expand Down
29 changes: 19 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,25 @@ module.exports = class IO {
* @param app <Koa app> the koa app to use
*/
attach( app ) {
if ( app.server || app._io ) {

if ( app.server && app.server.constructor.name != 'Server' ) {
throw new Error( 'app.server already exists but it\'s not an http server' );
}

if ( !app.server ) {
// Create a server if it doesn't already exists
app.server = http.createServer( app.callback() )

// Add warning to conventional .listen
// @TODO should this just be removed?
app.__listen = app.listen
app.listen = function listen() {
console.warn( 'IO is attached, did you mean app.server.listen()' )
return app.__listen.apply( app, arguments )
}
}

if ( app._io ) {
// Without a namespace we’ll use the default, but .io already exists meaning
// the default is taken already
if ( !this.opts.namespace ) {
Expand All @@ -108,19 +126,10 @@ module.exports = class IO {
return
}

// Add warning to conventional .listen
// @TODO should this just be removed?
app.__listen = app.listen
app.listen = function listen() {
console.warn( 'IO is attached, did you mean app.server.listen()' )
return app.__listen.apply( app, arguments )
}

if ( this.opts.hidden && !this.opts.namespace ) {
throw new Error( 'Default namespace can not be hidden' )
}

app.server = http.createServer( app.callback() )
app._io = new socketIO( app.server, this.opts.ioOptions )

if ( this.opts.namespace ) {
Expand Down
15 changes: 13 additions & 2 deletions spec/attach.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ tape( 'should not alter a koa app that already has ._io unless called with a nam
}, null, 'calling .attach throws an error when ._io already exists without a namespace' )
})

tape( 'should not alter a koa app that already has .server', t => {
tape( 'should work with koa app that already has .server', t => {
t.plan( 1 )

const app = new Koa()
const socket = new IO()
app.server = http.createServer()
socket.attach( app )

t.ok( app.io, 'socket is attached to koa app' )
})

tape( 'shouldn\'t work if app.server exists but it\'s not an http server', t => {
t.plan( 1 )

const app = new Koa()
Expand All @@ -44,7 +55,7 @@ tape( 'should not alter a koa app that already has .server', t => {

t.throws( () => {
socket.attach( app )
}, null, 'calling .attach throws an error when .server already exists' )
}, null, 'calling .attach throws an error when .server already exists but it\'s not an http server' )
})

tape( 'Attaching a namespace to a koa app with socket.io existing is all cool', t => {
Expand Down

0 comments on commit 038d72e

Please sign in to comment.