annotate node_modules/socket.io/Readme.md @ 101:52e44ee1c791 tip master

enabled all scores in autostart script
author Rob Canning <rc@kiben.net>
date Tue, 21 Apr 2015 16:20:57 +0100
parents 0ae87af84e2f
children
rev   line source
rc-web@69 1
rob@76 2 # socket.io
rc-web@69 3
rob@76 4 [![Build Status](https://secure.travis-ci.org/Automattic/socket.io.png)](http://travis-ci.org/Automattic/socket.io)
rob@76 5 [![NPM version](https://badge.fury.io/js/socket.io.png)](http://badge.fury.io/js/socket.io)
rc-web@69 6
rc-web@69 7 ## How to use
rc-web@69 8
rob@76 9 The following example attaches socket.io to a plain Node.JS
rob@76 10 HTTP server listening on port `3000`.
rc-web@69 11
rc-web@69 12 ```js
rob@76 13 var server = require('http').Server();
rob@76 14 var io = require('socket.io')(server);
rob@76 15 io.on('connection', function(socket){
rob@76 16 socket.on('event', function(data){});
rob@76 17 socket.on('disconnect', function(){});
rob@76 18 });
rob@76 19 server.listen(3000);
rc-web@69 20 ```
rc-web@69 21
rob@76 22 ### Standalone
rc-web@69 23
rc-web@69 24 ```js
rob@76 25 var io = require('socket.io')();
rob@76 26 io.on('connection', function(socket){});
rob@76 27 io.listen(3000);
rc-web@69 28 ```
rc-web@69 29
rob@76 30 ### In conjunction with Express
rob@76 31
rob@76 32 Starting with **3.0**, express applications have become request handler
rob@76 33 functions that you pass to `http` or `http` `Server` instances. You need
rob@76 34 to pass the `Server` to `socket.io`, and not the express application
rob@76 35 function.
rc-web@69 36
rc-web@69 37 ```js
rob@76 38 var app = require('express')();
rob@76 39 var server = require('http').Server(app);
rob@76 40 var io = require('socket.io')(server);
rob@76 41 io.on('connection', function(){ /* … */ });
rob@76 42 server.listen(3000);
rc-web@69 43 ```
rc-web@69 44
rob@76 45 ### In conjunction with Koa
rc-web@69 46
rob@76 47 Like Express.JS, Koa works by exposing an application as a request
rob@76 48 handler function, but only by calling the `callback` method.
rob@76 49
rob@76 50 ```js
rob@76 51 var app = require('koa')();
rob@76 52 var server = require('http').Server(app.callback());
rob@76 53 var io = require('socket.io')(server);
rob@76 54 io.on('connection', function(){ /* … */ });
rob@76 55 server.listen(3000);
rc-web@69 56 ```
rc-web@69 57
rob@76 58 ## API
rc-web@69 59
rob@76 60 ### Server
rc-web@69 61
rob@76 62 Exposed by `require('socket.io')`.
rc-web@69 63
rob@76 64 ### Server()
rc-web@69 65
rob@76 66 Creates a new `Server`. Works with and without `new`:
rc-web@69 67
rob@76 68 ```js
rob@76 69 var io = require('socket.io')();
rob@76 70 // or
rob@76 71 var Server = require('socket.io');
rob@76 72 var io = new Server();
rob@76 73 ```
rc-web@69 74
rob@76 75 ### Server(opts:Object)
rob@76 76
rob@76 77 Optionally, the first or second argument (see below) of the `Server`
rob@76 78 constructor can be an options object.
rob@76 79
rob@76 80 The following options are supported:
rob@76 81
rob@76 82 - `serveClient` sets the value for Server#serveClient()
rob@76 83 - `path` sets the value for Server#path()
rob@76 84
rob@76 85 The same options passed to socket.io are always passed to
rob@76 86 the `engine.io` `Server` that gets created. See engine.io
rob@76 87 [options](https://github.com/learnboost/engine.io#methods-1)
rob@76 88 as reference.
rob@76 89
rob@76 90 ### Server(srv:http#Server, opts:Object)
rob@76 91
rob@76 92 Creates a new `Server` and attaches it to the given `srv`. Optionally
rob@76 93 `opts` can be passed.
rob@76 94
rob@76 95 ### Server(port:Number, opts:Object)
rob@76 96
rob@76 97 Binds socket.io to a new `http.Server` that listens on `port`.
rob@76 98
rob@76 99 ### Server#serveClient(v:Boolean):Server
rob@76 100
rob@76 101 If `v` is `true` the attached server (see `Server#attach`) will serve
rob@76 102 the client files. Defaults to `true`.
rob@76 103
rob@76 104 This method has no effect after `attach` is called.
rob@76 105
rob@76 106 ```js
rob@76 107 // pass a server and the `serveClient` option
rob@76 108 var io = require('socket.io')(http, { serveClient: false });
rob@76 109
rob@76 110 // or pass no server and then you can call the method
rob@76 111 var io = require('socket.io')();
rob@76 112 io.serveClient(false);
rob@76 113 io.attach(http);
rob@76 114 ```
rob@76 115
rob@76 116 If no arguments are supplied this method returns the current value.
rob@76 117
rob@76 118 ### Server#path(v:String):Server
rob@76 119
rob@76 120 Sets the path `v` under which `engine.io` and the static files will be
rob@76 121 served. Defaults to `/socket.io`.
rob@76 122
rob@76 123 If no arguments are supplied this method returns the current value.
rob@76 124
rob@76 125 ### Server#adapter(v:Adapter):Server
rob@76 126
rob@76 127 Sets the adapter `v`. Defaults to an instance of the `Adapter` that
rob@76 128 ships with socket.io which is memory based. See
rob@76 129 [socket.io-adapter](https://github.com/Automattic/socket.io-adapter).
rob@76 130
rob@76 131 If no arguments are supplied this method returns the current value.
rob@76 132
rob@76 133 ### Server#origins(v:String):Server
rob@76 134
rob@76 135 Sets the allowed origins `v`. Defaults to any origins being allowed.
rob@76 136
rob@76 137 If no arguments are supplied this method returns the current value.
rob@76 138
rob@76 139
rob@76 140 ### Server#sockets:Namespace
rob@76 141
rob@76 142 The default (`/`) namespace.
rob@76 143
rob@76 144 ### Server#attach(srv:http#Server, opts:Object):Server
rob@76 145
rob@76 146 Attaches the `Server` to an engine.io instance on `srv` with the
rob@76 147 supplied `opts` (optionally).
rob@76 148
rob@76 149 ### Server#attach(port:Number, opts:Object):Server
rob@76 150
rob@76 151 Attaches the `Server` to an engine.io instance that is bound to `port`
rob@76 152 with the given `opts` (optionally).
rob@76 153
rob@76 154 ### Server#listen
rob@76 155
rob@76 156 Synonym of `Server#attach`.
rob@76 157
rob@76 158 ### Server#bind(srv:engine#Server):Server
rob@76 159
rob@76 160 Advanced use only. Binds the server to a specific engine.io `Server`
rob@76 161 (or compatible API) instance.
rob@76 162
rob@76 163 ### Server#onconnection(socket:engine#Socket):Server
rob@76 164
rob@76 165 Advanced use only. Creates a new `socket.io` client from the incoming
rob@76 166 engine.io (or compatible API) `socket`.
rob@76 167
rob@76 168 ### Server#of(nsp:String):Namespace
rob@76 169
rob@76 170 Initializes and retrieves the given `Namespace` by its pathname
rob@76 171 identifier `nsp`.
rob@76 172
rob@76 173 If the namespace was already initialized it returns it right away.
rob@76 174
rob@76 175 ### Server#emit
rob@76 176
rob@76 177 Emits an event to all connected clients. The following two are
rob@76 178 equivalent:
rob@76 179
rob@76 180 ```js
rob@76 181 var io = require('socket.io')();
rob@76 182 io.sockets.emit('an event sent to all connected clients');
rob@76 183 io.emit('an event sent to all connected clients');
rob@76 184 ```
rob@76 185
rob@76 186 For other available methods, see `Namespace` below.
rob@76 187
rob@76 188 ### Server#use
rob@76 189
rob@76 190 See `Namespace#use` below.
rob@76 191
rob@76 192 ### Namespace
rob@76 193
rob@76 194 Represents a pool of sockets connected under a given scope identified
rob@76 195 by a pathname (eg: `/chat`).
rob@76 196
rob@76 197 By default the client always connects to `/`.
rob@76 198
rob@76 199 #### Events
rob@76 200
rob@76 201 - `connection` / `connect`. Fired upon a connection.
rob@76 202
rob@76 203 Parameters:
rob@76 204 - `Socket` the incoming socket.
rob@76 205
rob@76 206 ### Namespace#name:String
rob@76 207
rob@76 208 The namespace identifier property.
rob@76 209
rob@76 210 ### Namespace#connected:Object<Socket>
rob@76 211
rob@76 212 Hash of `Socket` objects that are connected to this namespace indexed
rob@76 213 by `id`.
rob@76 214
rob@76 215 ### Namespace#use(fn:Function):Namespace
rob@76 216
rob@76 217 Registers a middleware, which is a function that gets executed for
rob@76 218 every incoming `Socket` and receives as parameter the socket and a
rob@76 219 function to optionally defer execution to the next registered
rob@76 220 middleware.
rob@76 221
rob@76 222 ```js
rob@76 223 var io = require('socket.io')();
rob@76 224 io.use(function(socket, next){
rob@76 225 if (socket.request.headers.cookie) return next();
rob@76 226 next(new Error('Authentication error'));
rc-web@69 227 });
rob@76 228 ```
rc-web@69 229
rob@76 230 Errors passed to middleware callbacks are sent as special `error`
rob@76 231 packets to clients.
rob@76 232
rob@76 233 ### Socket
rob@76 234
rob@76 235 A `Socket` is the fundamental class for interacting with browser
rob@76 236 clients. A `Socket` belongs to a certain `Namespace` (by default `/`)
rob@76 237 and uses an underlying `Client` to communicate.
rob@76 238
rob@76 239 ### Socket#rooms:Array
rob@76 240
rob@76 241 A list of strings identifying the rooms this socket is in.
rob@76 242
rob@76 243 ### Socket#client:Client
rob@76 244
rob@76 245 A reference to the underlying `Client` object.
rob@76 246
rob@76 247 ### Socket#conn:Socket
rob@76 248
rob@76 249 A reference to the underyling `Client` transport connection (engine.io
rob@76 250 `Socket` object).
rob@76 251
rob@76 252 ### Socket#request:Request
rob@76 253
rob@76 254 A getter proxy that returns the reference to the `request` that
rob@76 255 originated the underlying engine.io `Client`. Useful for accessing
rob@76 256 request headers such as `Cookie` or `User-Agent`.
rob@76 257
rob@76 258 ### Socket#id:String
rob@76 259
rob@76 260 A unique identifier for the socket session, that comes from the
rob@76 261 underlying `Client`.
rob@76 262
rob@76 263 ### Socket#emit(name:String[, …]):Socket
rob@76 264
rob@76 265 Emits an event to the socket identified by the string `name`. Any
rob@76 266 other parameters can be included.
rob@76 267
rob@76 268 All datastructures are supported, including `Buffer`. JavaScript
rob@76 269 functions can't be serialized/deserialized.
rob@76 270
rob@76 271 ```js
rob@76 272 var io = require('socket.io')();
rob@76 273 io.on('connection', function(socket){
rob@76 274 socket.emit('an event', { some: 'data' });
rc-web@69 275 });
rob@76 276 ```
rob@76 277
rob@76 278 ### Socket#join(name:String[, fn:Function]):Socket
rob@76 279
rob@76 280 Adds the socket to the `room`, and fires optionally a callback `fn`
rob@76 281 with `err` signature (if any).
rob@76 282
rob@76 283 The socket is automatically a member of a room identified with its
rob@76 284 session id (see `Socket#id`).
rob@76 285
rob@76 286 The mechanics of joining rooms are handled by the `Adapter`
rob@76 287 that has been configured (see `Server#adapter` above), defaulting to
rob@76 288 [socket.io-adapter](https://github.com/Automattic/socket.io-adapter).
rob@76 289
rob@76 290 ### Socket#leave(name:String[, fn:Function]):Socket
rob@76 291
rob@76 292 Removes the socket from `room`, and fires optionally a callback `fn`
rob@76 293 with `err` signature (if any).
rob@76 294
rob@76 295 **Rooms are left automatically upon disconnection**.
rob@76 296
rob@76 297 The mechanics of leaving rooms are handled by the `Adapter`
rob@76 298 that has been configured (see `Server#adapter` above), defaulting to
rob@76 299 [socket.io-adapter](https://github.com/Automattic/socket.io-adapter).
rob@76 300
rob@76 301 ### Socket#to(room:String):Socket
rob@76 302 ### Socket#in(room:String):Socket
rob@76 303
rob@76 304 Sets a modifier for a subsequent event emission that the event will
rob@76 305 only be _broadcasted_ to sockets that have joined the given `room`.
rob@76 306
rob@76 307 To emit to multiple rooms, you can call `to` several times.
rob@76 308
rob@76 309 ```js
rob@76 310 var io = require('socket.io')();
rob@76 311 io.on('connection', function(socket){
rob@76 312 socket.to('others').emit('an event', { some: 'data' });
rob@76 313 });
rob@76 314 ```
rob@76 315
rob@76 316 ### Client
rob@76 317
rob@76 318 The `Client` class represents an incoming transport (engine.io)
rob@76 319 connection. A `Client` can be associated with many multiplexed `Socket`
rob@76 320 that belong to different `Namespace`s.
rob@76 321
rob@76 322 ### Client#conn
rob@76 323
rob@76 324 A reference to the underlying `engine.io` `Socket` connection.
rob@76 325
rob@76 326 ### Client#request
rob@76 327
rob@76 328 A getter proxy that returns the reference to the `request` that
rob@76 329 originated the engine.io connection. Useful for accessing
rob@76 330 request headers such as `Cookie` or `User-Agent`.
rob@76 331
rob@76 332 ## Debug / logging
rob@76 333
rob@76 334 Socket.IO is powered by [debug](http://github.com/visionmedia/debug).
rob@76 335 In order to see all the debug output, run your app with the environment variable
rob@76 336 `DEBUG` including the desired scope.
rob@76 337
rob@76 338 To see the output from all of Socket.IO's debugging scopes you can use:
rob@76 339
rob@76 340 ```
rob@76 341 DEBUG=socket.io* node myapp
rc-web@69 342 ```
rc-web@69 343
rob@76 344 ## License
rc-web@69 345
rob@76 346 MIT