Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 344 lines (252 sloc) 8.668 kB
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
1 # Socket.IO
2
3 Socket.IO is a Node.JS project that makes WebSockets and realtime possible in
4 all browsers. It also enhances WebSockets by providing built-in multiplexing,
5 horizontal scalability, automatic JSON encoding/decoding, and more.
6
7 ## How to Install
8
70c815d @rauchg Modifications to README.
rauchg authored
9 npm install socket.io
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
10
11 ## How to use
12
70c815d @rauchg Modifications to README.
rauchg authored
13 First, require `socket.io`:
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
14
94bc2bc @rauchg Trying out GH highlighting.
rauchg authored
15 ```js
16 var io = require('socket.io');
17 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
18
19 Next, attach it to a HTTP/HTTPS server. If you're using the fantastic `express`
20 web framework:
21
005f251 @rauchg Trying out GH highlighting.
rauchg authored
22 ```js
27ab98d @ericz Fixed typo
ericz authored
23 var app = express.createServer()
94bc2bc @rauchg Trying out GH highlighting.
rauchg authored
24 , io = io.listen(app);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
25
94bc2bc @rauchg Trying out GH highlighting.
rauchg authored
26 app.listen(80);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
27
94bc2bc @rauchg Trying out GH highlighting.
rauchg authored
28 io.sockets.on('connection', function (socket) {
159ad73 @rauchg Fixed readme
rauchg authored
29 socket.emit('news', { hello: 'world' });
30 socket.on('my other event', function (data) {
31 console.log(data);
32 });
94bc2bc @rauchg Trying out GH highlighting.
rauchg authored
33 });
34 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
35
36 Finally, load it from the client side code:
37
005f251 @rauchg Trying out GH highlighting.
rauchg authored
38 ```html
39 <script src="/socket.io/socket.io.js"></script>
40 <script>
41 var socket = io.connect('http://localhost');
159ad73 @rauchg Fixed readme
rauchg authored
42 socket.on('news', function (data) {
43 console.log(data);
44 socket.emit('my other event', { my: 'data' });
005f251 @rauchg Trying out GH highlighting.
rauchg authored
45 });
46 </script>
47 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
48
49 For more thorough examples, look at the `examples/` directory.
50
51 ## Short recipes
52
53 ### Sending and receiving events.
54
55 Socket.IO allows you to emit and receive custom events.
56 Besides `connect`, `message` and `disconnect`, you can emit custom events:
57
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
58 ```js
59 // note, io.listen(<port>) will create a http server for you
60 var io = require('socket.io').listen(80);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
61
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
62 io.sockets.on('connection', function (socket) {
ec88f95 @Znarkus Fixed typo.
Znarkus authored
63 io.sockets.emit('this', { will: 'be received by everyone' });
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
64
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
65 socket.on('private message', function (from, msg) {
66 console.log('I received a private message by ', from, ' saying ', msg);
67 });
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
68
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
69 socket.on('disconnect', function () {
acdbacb @chees Changed sockets.emit to io.sockets.emit to get the example working.
chees authored
70 io.sockets.emit('user disconnected');
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
71 });
72 });
73 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
74
75 ### Storing data associated to a client
76
77 Sometimes it's necessary to store data associated with a client that's
78 necessary for the duration of the session.
79
80 #### Server side
81
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
82 ```js
83 var io = require('socket.io').listen(80);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
84
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
85 io.sockets.on('connection', function (socket) {
86 socket.on('set nickname', function (name) {
87 socket.set('nickname', name, function () { socket.emit('ready'); });
88 });
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
89
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
90 socket.on('msg', function () {
c9bf814 @3rd-Eden Readme fixes
3rd-Eden authored
91 socket.get('nickname', function (err, name) {
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
92 console.log('Chat message by ', name);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
93 });
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
94 });
95 });
96 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
97
98 #### Client side
99
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
100 ```html
101 <script>
102 var socket = io.connect('http://localhost');
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
103
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
104 socket.on('connect', function () {
105 socket.emit('set nickname', confirm('What is your nickname?'));
106 socket.on('ready', function () {
107 console.log('Connected !');
108 socket.emit('msg', confirm('What is your message?'));
109 });
110 });
111 </script>
112 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
113
114 ### Restricting yourself to a namespace
115
116 If you have control over all the messages and events emitted for a particular
117 application, using the default `/` namespace works.
118
119 If you want to leverage 3rd-party code, or produce code to share with others,
120 socket.io provides a way of namespacing a `socket`.
121
122 This has the benefit of `multiplexing` a single connection. Instead of
123 socket.io using two `WebSocket` connections, it'll use one.
124
125 The following example defines a socket that listens on '/chat' and one for
126 '/news':
127
128 #### Server side
129
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
130 ```js
131 var io = require('socket.io').listen(80);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
132
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
133 var chat = io
b37666a @jherdman Minor documentation fix
jherdman authored
134 .of('/chat')
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
135 .on('connection', function (socket) {
136 socket.emit('a message', { that: 'only', '/chat': 'will get' });
137 chat.emit('a message', { everyone: 'in', '/chat': 'will get' });
138 });
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
139
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
140 var news = io
141 .of('/news');
142 .on('connection', function (socket) {
143 socket.emit('item', { news: 'item' });
144 });
145 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
146
147 #### Client side:
148
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
149 ```html
150 <script>
a00ce7e @rauchg Fix for client side namespaces.
rauchg authored
151 var chat = io.connect('http://localhost/chat')
152 , news = io.connect('http://localhost/news');
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
153
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
154 chat.on('connect', function () {
155 chat.emit('hi!');
156 });
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
157
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
158 news.on('news', function () {
159 news.emit('woot');
160 });
161 </script>
162 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
163
164 ### Sending volatile messages.
165
166 Sometimes certain messages can be dropped. Let's say you have an app that
167 shows realtime tweets for the keyword `bieber`.
168
169 If a certain client is not ready to receive messages (because of network slowness
170 or other issues, or because he's connected through long polling and is in the
171 middle of a request-response cycle), if he doesn't receive ALL the tweets related
172 to bieber your application won't suffer.
173
174 In that case, you might want to send those messages as volatile messages.
175
176 #### Server side
177
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
178 ```js
179 var io = require('socket.io').listen(80);
8f1d96a @3rd-Eden Updated the readme, fixed typos, changed `socket.io` requires to `soc…
3rd-Eden authored
180
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
181 io.sockets.on('connection', function (socket) {
182 var tweets = setInterval(function () {
183 getBieberTweet(function (tweet) {
184 socket.volatile.emit('bieber tweet', tweet);
9cf4008 @rauchg Fixes for README
rauchg authored
185 });
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
186 }, 100);
187
188 socket.on('disconnect', function () {
189 clearInterval(tweets);
190 });
191 });
192 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
193
194 #### Client side
195
196 In the client side, messages are received the same way whether they're volatile
197 or not.
198
199 ### Getting acknowledgements
200
201 Sometimes, you might want to get a callback when the client confirmed the message
8f1d96a @3rd-Eden Updated the readme, fixed typos, changed `socket.io` requires to `soc…
3rd-Eden authored
202 reception.
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
203
204 To do this, simply pass a function as the last parameter of `.send` or `.emit`.
1db72e1 @rauchg Simplified acks.
rauchg authored
205 What's more, when you use `.emit`, the acknowledgement is done by you, which
206 means you can also pass data along:
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
207
208 #### Server side
209
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
210 ```js
211 var io = require('socket.io').listen(80);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
212
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
213 io.sockets.on('connection', function (socket) {
214 socket.on('ferret', function (name, fn) {
215 fn('woot');
216 });
217 });
218 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
219
220 #### Client side
221
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
222 ```html
223 <script>
224 var socket = io.connect(); // TIP: .connect with no args does auto-discovery
21c0155 @rauchg Added tip [getify]
rauchg authored
225 socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
226 socket.emit('ferret', 'tobi', function (data) {
227 console.log(data); // data will be 'woot'
228 });
229 });
230 </script>
231 ```
9cf4008 @rauchg Fixes for README
rauchg authored
232
233 ### Broadcasting messages
234
235 To broadcast, simply add a `broadcast` flag to `emit` and `send` method calls.
2838f25 @rauchg Some readme fixes. Broadcast clarification, added rooms.
rauchg authored
236 Broadcasting means sending a message to everyone else except for the socket
237 that starts it.
9cf4008 @rauchg Fixes for README
rauchg authored
238
239 #### Server side
240
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
241 ```js
242 var io = require('socket.io').listen(80);
9cf4008 @rauchg Fixes for README
rauchg authored
243
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
244 io.sockets.on('connection', function (socket) {
245 socket.broadcast.emit('user connected');
246 socket.broadcast.json.send({ a: 'message' });
247 });
248 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
249
2838f25 @rauchg Some readme fixes. Broadcast clarification, added rooms.
rauchg authored
250 ### Rooms
251
252 Sometimes you want to put certain sockets in the same room, so that it's easy
253 to broadcast to all of them together.
254
255 Think of this as built-in channels for sockets. Sockets `join` and `leave`
256 rooms in each socket.
257
258 #### Server side
259
260 ```js
261 var io = require('socket.io').listen(80);
262
263 io.sockets.on('connection', function (socket) {
264 socket.join('justin bieber fans');
265 socket.broadcast.to('justin bieber fans').emit('new fan');
266 io.sockets.in('rammstein fans').emit('new non-fan');
267 });
268 ```
269
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
270 ### Using it just as a cross-browser WebSocket
271
272 If you just want the WebSocket semantics, you can do that too.
273 Simply leverage `send` and listen on the `message` event:
274
275 #### Server side
276
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
277 ```js
c9bf814 @3rd-Eden Readme fixes
3rd-Eden authored
278 var io = require('socket.io').listen(80);
42a4e6b @rauchg Updated README
rauchg authored
279
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
280 io.sockets.on('connection', function (socket) {
281 socket.on('message', function () { });
282 socket.on('disconnect', function () { });
283 });
284 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
285
286 #### Client side
287
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
288 ```html
289 <script>
290 var socket = io.connect('http://localhost/');
291 socket.on('connect', function () {
292 socket.send('hi');
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
293
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
294 socket.on('message', function (msg) {
295 // my msg
296 });
297 });
298 </script>
299 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
300
301 ### Changing configuration
302
303 Configuration in socket.io is TJ-style:
304
305 #### Server side
306
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
307 ```js
c9bf814 @3rd-Eden Readme fixes
3rd-Eden authored
308 var io = require('socket.io').listen(80);
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
309
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
310 io.configure(function () {
311 io.set('transports', ['websocket', 'flashsocket', 'xhr-polling']);
312 });
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
313
aafbf85 @rauchg Trading vim highlighting with pretty github highlighting.
rauchg authored
314 io.configure('development', function () {
315 io.set('transports', ['websocket', 'xhr-polling']);
316 io.enable('log');
317 });
318 ```
1514af0 @rauchg Socket.IO 0.7-pre
rauchg authored
319
320 ## License
321
322 (The MIT License)
323
324 Copyright (c) 2011 Guillermo Rauch &lt;guillermo@learnboost.com&gt;
325
326 Permission is hereby granted, free of charge, to any person obtaining
327 a copy of this software and associated documentation files (the
328 'Software'), to deal in the Software without restriction, including
329 without limitation the rights to use, copy, modify, merge, publish,
330 distribute, sublicense, and/or sell copies of the Software, and to
331 permit persons to whom the Software is furnished to do so, subject to
332 the following conditions:
333
334 The above copyright notice and this permission notice shall be
335 included in all copies or substantial portions of the Software.
336
337 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
338 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
339 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
340 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
341 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
342 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
343 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.