Koa-socket
Sugar for connecting socket.io to a koa instance
Koa-socket is now compatible with koa v2 style of middleware (where context is passed as a parameter), v0.4.0 of koa-socket is the last version to support the old style of middleware.
As such, koa-socket now requires node v4.0.0 or higher although koa-socket simply attaches to the server instance so will be compatible with a koa v1 powered app.
Installation
npm i -S koa-socket
Example
const Koa = const IO = const app = const io = app io io app
Features
- Attach socket.io to existing koa projects
- Attach koa-style middleware to socket.io events
- Supports koa v2 style of passing context along the response chain
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.
It also re-maps app.listen
to app.server.listen
, so you could simply do app.listen()
. However if you already had an app.server
attached, it uses it instead and expects you to do app.server.listen()
yourself.
const Koa = const IO = const app = const io = // Attach the socket to the applicationio // Socket is now available as app.io if you preferappio // The raw socket.io instance is attached as app._io if you need itapp_io // app.listen is mapped to app.server.listen, so you can just do:app // *If* you had manually attached an `app.server` yourself, you should do:appserver
Middleware and event handlers
Middleware can be added in much the same way as it can be added to any regular koa instance.
Example with async functions (transpilation required)
io
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.
Example with generator functions
Koa v2 no longer supports generators so if you are using v2 then you must use co.wrap
to have access to the generator style.
const Koa = const IO = const co = const app = const io = app io io; io ioapp;
Plain example
Whilst slightly unwieldy, the standalone method also works
io
Passed Context
let ctx = event: listenerevent data: data socket: Socket acknowledge: cb
The context passed to each socket middleware and handler begins the chain with the event that triggered the response, the data sent with that event and the socket instance that is handling the event. There is also a shorthand for firing an acknowledgement back to the client.
As the context is passed to each function in the response chain it is fair game for mutation at any point along that chain, it is up to you to decide whether this is an anti-pattern or not. There was much discussion around this topic for koa v2.
io io io
Namespaces
Namespaces can be defined simply by instantiating a new instance of koaSocket
and passing the namespace id in the constructor. All other functionality works the same, it’ll just be constrained to the single namespace.
const app = const chat = namespace: 'chat' chat chat
Namespaces also attach themselves to the app
instance, throwing an error if the property name already exists.
const app = const chat = namespace: 'chat' chat appchatappchatappchat
The attachment is configurable if you don’t want to muddy the app
object with all your namespaces.
const chat = namespace: 'chat' hidden: true chatchat
Namespaces are fairly ubiquitous so they get a dirty shorthand for creating them, note that if you want to add any additional options you’ll need to use the longhand object parameter to instantiate koaSocket
.
const chat = 'chat'
API
Koa app
)
.attach( Attaches to a koa application
ioapp
Function callback
)
.use( Applies middleware to the stack.
Middleware are executed each time an event is reacted to and before the callback is triggered for an event.
Middleware with generators should use co.wrap
.
Middleware functions are called with ctx
and next
. The context is passed through each middleware and out to the event listener callback. next
allows the middleware chain to be traversed. Under the hood koa-compose
is used to follow functionality with koa
.
io
String event
, Function callback
)
.on( Attaches a callback to an event.
The callback is fired after any middleware that are attached to the instance and is called with the ctx
object and the data
that triggered the event. The data
can also be found on the ctx
, the only potential difference is that data
is the raw data
emitted with the event trigger whilst ctx.data
could have been mutated within the middleware stack.
io
String event
, Function callback
)
.off( Removes a callback from an event.
If the event
is omitted then it will remove all listeners from the instance.
If the callback
is omitted then all callbacks for the supplied event will be removed.
ioioio
String event
, data
)
.broadcast( Sends a message to all connections.
Running tests
npm test
License
MIT