This gives details on the new top level objects for express.io.
express = require('express.io')
express()
- Creates a new app object.express.io
- The socket.io object.
app = require('express.io')()
app.http()
- starts an http server, returnsapp
app.https(options)
- starts an https server, returnsapp
app.io()
- starts an io server, returnsapp
For a complete list of properties, please check the docs.
The io object for the entire app. Used for routing and broadcasting to clients.
app.io.broadcast('hey', {this: 'goes to everyone!'})
app.io.room('hipster').broadcast('meh', {this: 'goes to all hipsters'})
app.io.route('special', function(req) {
// do something with req
})
You can also use the AppIO
object to configure your io server. For available options, check here.
app.io.configure(function() {
app.io.enable('browser client minification'); // send minified client
app.io.enable('browser client gzip'); // gzip the file
app.io.set('log level', 1); // reduce logging
})
Note: You must call app.io()
before using.
app.io.broadcast(event, data)
- Broadcast theevent
anddata
to all clients.app.io.room(room).broadcast(event, data)
- Broadcast theevent
anddata
only to clients in theroom
.app.io.route(event, callback)
- Takes aroute
name and acallback
. The callback passesreq
, which is aSocketRequest
object.app.io.set(property, value)
- Set a global io server property.app.io.enable(property)
- Enable an io server feature.app.io.configure(environment)
- Similar to theapp.configure
method for express.
This object is passed to the io routes.
app.io.route('hello', function(req) {
// do something with req
})
req.data
- Thedata
sent from the client request.req.io
- TheRequestIO
object for the request.req.headers
-headers
from the initial web socket request.req.session
- If you have sessions, then this is the expresssession
object.req.handshake
- This is the iohandshake
data.req.socket
- The actual socket.iosocket
. Please usereq.io
instead.
This object comes with the SocketRequest
, and it gives you access to the request io.
app.io.route('example', function(req) {
req.io.emit('event', {this: 'is sent as an event to the client'})
req.io.broadcast('shout-out', {this: 'goes to every client, except this one'})
req.io.respond({sends: 'this data back to the client as an acknowledgment'})
req.io.join('hipster') // joins the hipster room
req.io.leave('hipster') // leaves the hipster room
req.io.room('hipster').broadcast('hey', {this: 'goes to every hipster'})
req.io.route('some-other-route')
})
req.io.emit(event, data)
- Send anevent
withdata
to this client.req.io.respond(data)
- Sends the acknowledgmentdata
back to the client.req.io.broadcast(event, data)
- Broadcast to all clients except this one.req.io.room(room).broadcast(event,data)
- Broadcast to the specifiedroom
, theevent
anddata
. Every client in theroom
except the one making the request, receives thisevent
.req.io.join(room)
- Make the client join the specifiedroom
.req.io.leave(room)
- Make the client leave the specifiedroom
.req.io.route(route)
- Forwards thereq
to the givenroute
.
These events are reserved and should not be used with app.io.route
or req.io.on
unless you know what you are doing.
connect
connecting
disconnect
connect_failed
error
message
reconnect_failed
reconnect
reconnecting
View the docs for details on these events.