Skip to content

Commit

Permalink
#update docs for middleware signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstyles committed Nov 19, 2015
1 parent 9dace5b commit 3197950
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ app.server.listen( process.env.PORT || 3000 )

Middleware can be added to sockets in much the same way as it is added to a koa instance, currently only generator functions can be used and they must be used in the same way that koa v2 uses them i.e. with `co.wrap`.

### Example with generator functions

In the same way as koa v2 uses generator functions, you’ll need to wrap your function in `co.wrap`

```js
const Koa = require( 'koa' )
const IO = require( 'koa-socket' )
Expand All @@ -98,6 +102,31 @@ io.attach( app )
app.server.listen( 3000 );
```

### Example with *async* functions (transpilation required)

```js
io.use( async ( ctx, next ) {
let start = new Date()
await next()
console.log( `response time: ${ new Date() - start }ms` )
})
```

There is an example in the `examples` folder, use `npm run example-babel` to fire it up. The npm script relies on the `babel` require hook, which is not recommended in production.

### Plain example

Whilst slightly unwieldy, the standalone method also works

```js
io.use( ( ctx, next ) => {
let start = new Date()
return next().then( () => {
console.log( `response time: ${ new Date() - start }ms` )
})
})
```


## Passed Context

Expand Down

0 comments on commit 3197950

Please sign in to comment.