PageRenderTime 135ms CodeModel.GetById 26ms RepoModel.GetById 2ms app.codeStats 0ms

/PhoneGap02/node_modules/socket.io/Readme.md

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