|
| 1 | + |
| 2 | +# Socket.IO |
| 3 | + |
| 4 | +Socket.IO is a Node.JS project that makes WebSockets and realtime possible in |
| 5 | +all browsers. It also enhances WebSockets by providing built-in multiplexing, |
| 6 | +horizontal scalability, automatic JSON encoding/decoding, and more. |
| 7 | + |
| 8 | +## How to Install |
| 9 | + |
| 10 | + npm install socket.io |
| 11 | + |
| 12 | +## How to use |
| 13 | + |
| 14 | +First, require `socket.io`: |
| 15 | + |
| 16 | + var io = require('socket.io'); |
| 17 | + |
| 18 | +Next, attach it to a HTTP/HTTPS server. If you're using the fantastic `express` |
| 19 | +web framework: |
| 20 | + |
| 21 | + var app = express.createServer(); |
| 22 | + , sockets = io.listen(app); |
| 23 | + |
| 24 | + app.listen(80); |
| 25 | + |
| 26 | + sockets.on('connection', function (socket) { |
| 27 | + socket.send({ hello: 'world' }); |
| 28 | + }); |
| 29 | + |
| 30 | +Finally, load it from the client side code: |
| 31 | + |
| 32 | + <script src="/socket.io/socket.io.js"></script> |
| 33 | + <script> |
| 34 | + var socket = io.connect('http://localhost'); |
| 35 | + socket.on('news', function () { |
| 36 | + socket.emit('myOtherEvent', { my: 'data' }); |
| 37 | + }); |
| 38 | + </script> |
| 39 | + |
| 40 | +For more thorough examples, look at the `examples/` directory. |
| 41 | + |
| 42 | +## Short recipes |
| 43 | + |
| 44 | +### Sending and receiving events. |
| 45 | + |
| 46 | +Socket.IO allows you to emit and receive custom events. |
| 47 | +Besides `connect`, `message` and `disconnect`, you can emit custom events: |
| 48 | + |
| 49 | + // note, io.listen(<port>) will create a http server for you |
| 50 | + var io = require('socket.io'); |
| 51 | + , sockets = io.listen(80); |
| 52 | + |
| 53 | + sockets.on('connection', function (socket) { |
| 54 | + sockets.emit('this', { will: 'be received by everyone'); |
| 55 | + |
| 56 | + socket.on('private message', function (from, msg) { |
| 57 | + console.log('I received a private message by ', from, ' saying ', msg); |
| 58 | + }); |
| 59 | + |
| 60 | + socket.on('disconnect', function () { |
| 61 | + sockets.emit('user disconnected'); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | +### Storing data associated to a client |
| 66 | + |
| 67 | +Sometimes it's necessary to store data associated with a client that's |
| 68 | +necessary for the duration of the session. |
| 69 | + |
| 70 | +#### Server side |
| 71 | + |
| 72 | + var io = require('socket.io') |
| 73 | + , sockets = io.listen(80); |
| 74 | + |
| 75 | + sockets.on('connection', function (socket) { |
| 76 | + socket.on('set nickname', function (name) { |
| 77 | + socket.set('nickname', name, function () { socket.emit('ready'); }); |
| 78 | + }); |
| 79 | + |
| 80 | + socket.on('msg', function () { |
| 81 | + socket.get('nickname', function (name) { |
| 82 | + console.log('Chat message by ', name); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | +#### Client side |
| 88 | + |
| 89 | + <script> |
| 90 | + var socket = io.connect('http://localhost'); |
| 91 | + |
| 92 | + socket.on('connect', function () { |
| 93 | + socket.emit('set nickname', confirm('What is your nickname?')); |
| 94 | + socket.on('ready', function () { |
| 95 | + console.log('Connected !'); |
| 96 | + socket.emit('msg', confirm('What is your message?')); |
| 97 | + }); |
| 98 | + }); |
| 99 | + </script> |
| 100 | + |
| 101 | +### Restricting yourself to a namespace |
| 102 | + |
| 103 | +If you have control over all the messages and events emitted for a particular |
| 104 | +application, using the default `/` namespace works. |
| 105 | + |
| 106 | +If you want to leverage 3rd-party code, or produce code to share with others, |
| 107 | +socket.io provides a way of namespacing a `socket`. |
| 108 | + |
| 109 | +This has the benefit of `multiplexing` a single connection. Instead of |
| 110 | +socket.io using two `WebSocket` connections, it'll use one. |
| 111 | + |
| 112 | +The following example defines a socket that listens on '/chat' and one for |
| 113 | +'/news': |
| 114 | + |
| 115 | +#### Server side |
| 116 | + |
| 117 | + var io = require('socket.io') |
| 118 | + , sockets = io.listen(80); |
| 119 | + |
| 120 | + var chat = sockets |
| 121 | + .for('/chat'); |
| 122 | + .on('connection', function (socket) { |
| 123 | + socket.emit('a message', { that: 'only', '/chat': 'will get' }); |
| 124 | + chat.emit('a message', { everyone: 'in', '/chat': 'will get' }); |
| 125 | + }); |
| 126 | + |
| 127 | + var news = sockets |
| 128 | + .for('/news'); |
| 129 | + .on('connection', function (socket) { |
| 130 | + socket.emit('item', { news: 'item' }); |
| 131 | + }); |
| 132 | + |
| 133 | +#### Client side: |
| 134 | + |
| 135 | + <script> |
| 136 | + var socket = io.connect('http://localhost/') |
| 137 | + , chat = socket.for('/chat') |
| 138 | + , news = socket.for('/news'); |
| 139 | + |
| 140 | + chat.on('connect', function () { |
| 141 | + chat.emit('hi!'); |
| 142 | + }); |
| 143 | + |
| 144 | + news.on('news', function () { |
| 145 | + news.emit('woot'); |
| 146 | + }); |
| 147 | + </script> |
| 148 | + |
| 149 | +### Sending volatile messages. |
| 150 | + |
| 151 | +Sometimes certain messages can be dropped. Let's say you have an app that |
| 152 | +shows realtime tweets for the keyword `bieber`. |
| 153 | + |
| 154 | +If a certain client is not ready to receive messages (because of network slowness |
| 155 | +or other issues, or because he's connected through long polling and is in the |
| 156 | +middle of a request-response cycle), if he doesn't receive ALL the tweets related |
| 157 | +to bieber your application won't suffer. |
| 158 | + |
| 159 | +In that case, you might want to send those messages as volatile messages. |
| 160 | + |
| 161 | +#### Server side |
| 162 | + |
| 163 | + var io = require('socket.io') |
| 164 | + , sockets = io.listen(80); |
| 165 | + |
| 166 | + sockets.on('connection', function (socket) { |
| 167 | + var tweets = setInterval(function () { |
| 168 | + getBieberTweet(function (tweet) { |
| 169 | + socket.volatile.emit('bieber tweeet', tweet); |
| 170 | + }); |
| 171 | + }, 100); |
| 172 | + |
| 173 | + socket.on('disconnect', function () { |
| 174 | + clearInterval(tweets); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | +#### Client side |
| 179 | + |
| 180 | +In the client side, messages are received the same way whether they're volatile |
| 181 | +or not. |
| 182 | + |
| 183 | +### Getting acknowledgements |
| 184 | + |
| 185 | +Sometimes, you might want to get a callback when the client confirmed the message |
| 186 | +receiption. |
| 187 | + |
| 188 | +To do this, simply pass a function as the last parameter of `.send` or `.emit`. |
| 189 | +What's more, you can also perform a manual acknowledgement, like in the example |
| 190 | +below. Socket.IO won't perform a manual acknowledgement when the arity of the |
| 191 | +function is `0` when you `emit` or `send`. |
| 192 | + |
| 193 | +#### Server side |
| 194 | + |
| 195 | + var io = require('socket.io') |
| 196 | + , sockets = io.listen(80); |
| 197 | + |
| 198 | + sockets.on('connection', function (socket) { |
| 199 | + socket.on('ferret', function (name, fn) { |
| 200 | + fn('woot'); |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | +#### Client side |
| 205 | + |
| 206 | + <script> |
| 207 | + var socket = io.connect(); // TIP: .connect with no args does auto-discovery |
| 208 | + socket.on('connection', function () { |
| 209 | + socket.emit('ferret', 'tobi', function (data) { |
| 210 | + // if the funtion arity here was 0 (ie: if no parameters were defined), |
| 211 | + // socket.io would handle the acknowledgement automatically. |
| 212 | + console.log(data); // data will be 'woot' |
| 213 | + }); |
| 214 | + }); |
| 215 | + </script> |
| 216 | + |
| 217 | +### Using it just as a cross-browser WebSocket |
| 218 | + |
| 219 | +If you just want the WebSocket semantics, you can do that too. |
| 220 | +Simply leverage `send` and listen on the `message` event: |
| 221 | + |
| 222 | +#### Server side |
| 223 | + |
| 224 | + var io = require('socket.io') |
| 225 | + , sockets = io.listen(80); |
| 226 | + |
| 227 | +#### Client side |
| 228 | + |
| 229 | + <script> |
| 230 | + var socket = io.connect('http://localhost/'); |
| 231 | + socket.on('connect', function () { |
| 232 | + socket.send('hi'); |
| 233 | + |
| 234 | + socket.on('message', function (msg) { |
| 235 | + // my msg |
| 236 | + }); |
| 237 | + }); |
| 238 | + </script> |
| 239 | + |
| 240 | +### Changing configuration |
| 241 | + |
| 242 | +Configuration in socket.io is TJ-style: |
| 243 | + |
| 244 | +#### Server side |
| 245 | + |
| 246 | + var io = require('socket.io') |
| 247 | + , sockets = io.listen(80); |
| 248 | + |
| 249 | + sockets.configure(function () { |
| 250 | + sockets.set('transports', ['websocket', 'flashsocket', 'xhr-polling']); |
| 251 | + }); |
| 252 | + |
| 253 | + sockets.configure('development', function () { |
| 254 | + sockets.set('transports', ['websocket', 'xhr-polling']); |
| 255 | + sockets.enable('log'); |
| 256 | + }); |
| 257 | + |
| 258 | +## [API docs](http://socket.io/api.html) |
| 259 | + |
| 260 | +## License |
| 261 | + |
| 262 | +(The MIT License) |
| 263 | + |
| 264 | +Copyright (c) 2011 Guillermo Rauch <[email protected]> |
| 265 | + |
| 266 | +Permission is hereby granted, free of charge, to any person obtaining |
| 267 | +a copy of this software and associated documentation files (the |
| 268 | +'Software'), to deal in the Software without restriction, including |
| 269 | +without limitation the rights to use, copy, modify, merge, publish, |
| 270 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 271 | +permit persons to whom the Software is furnished to do so, subject to |
| 272 | +the following conditions: |
| 273 | + |
| 274 | +The above copyright notice and this permission notice shall be |
| 275 | +included in all copies or substantial portions of the Software. |
| 276 | + |
| 277 | +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
| 278 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 279 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 280 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 281 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 282 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 283 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments