Click here to Skip to main content
15,880,725 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 356.3K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
{
  "name": "socket.io-client",
  "description": "Socket.IO client for the browser and node.js",
  "version": "0.9.11",
  "main": "./lib/io.js",
  "browserify": "./dist/socket.io.js",
  "homepage": "http://socket.io",
  "keywords": [
    "websocket",
    "socket",
    "realtime",
    "socket.io",
    "comet",
    "ajax"
  ],
  "author": {
    "name": "Guillermo Rauch",
    "email": "guillermo@learnboost.com"
  },
  "contributors": [
    {
      "name": "Guillermo Rauch",
      "email": "rauchg@gmail.com"
    },
    {
      "name": "Arnout Kazemier",
      "email": "info@3rd-eden.com"
    },
    {
      "name": "Vladimir Dronnikov",
      "email": "dronnikov@gmail.com"
    },
    {
      "name": "Einar Otto Stangvik",
      "email": "einaros@gmail.com"
    }
  ],
  "repository": {
    "type": "git",
    "url": "https://github.com/LearnBoost/socket.io-client.git"
  },
  "dependencies": {
    "uglify-js": "1.2.5",
    "ws": "0.4.x",
    "xmlhttprequest": "1.4.2",
    "active-x-obfuscator": "0.0.1"
  },
  "devDependencies": {
    "expresso": "*",
    "express": "2.5.x",
    "jade": "*",
    "stylus": "*",
    "socket.io": "0.9.11",
    "socket.io-client": "0.9.11",
    "should": "*"
  },
  "engines": {
    "node": ">= 0.4.0"
  },
  "readme": "socket.io\n=========\n\n#### Sockets for the rest of us\n\nThe `socket.io` client is basically a simple HTTP Socket interface implementation.\nIt looks similar to WebSocket while providing additional features and\nleveraging other transports when WebSocket is not supported by the user's\nbrowser.\n\n```js\nvar socket = io.connect('http://domain.com');\nsocket.on('connect', function () {\n  // socket connected\n});\nsocket.on('custom event', function () {\n  // server emitted a custom event\n});\nsocket.on('disconnect', function () {\n  // socket disconnected\n});\nsocket.send('hi there');\n```\n\n### Recipes\n\n#### Utilizing namespaces (ie: multiple sockets)\n\nIf you want to namespace all the messages and events emitted to a particular\nendpoint, simply specify it as part of the `connect` uri:\n\n```js\nvar chat = io.connect('http://localhost/chat');\nchat.on('connect', function () {\n  // chat socket connected\n});\n\nvar news = io.connect('/news'); // io.connect auto-detects host\nnews.on('connect', function () {\n  // news socket connected\n});\n```\n\n#### Emitting custom events\n\nTo ease with the creation of applications, you can emit custom events outside\nof the global `message` event.\n\n```js\nvar socket = io.connect();\nsocket.emit('server custom event', { my: 'data' });\n```\n\n#### Forcing disconnection\n\n```js\nvar socket = io.connect();\nsocket.on('connect', function () {\n  socket.disconnect();\n});\n```\n\n### Documentation \n\n#### io#connect\n\n```js\nio.connect(uri, [options]);\n```\n\n##### Options:\n\n- *resource*\n\n    socket.io\n\n  The resource is what allows the `socket.io` server to identify incoming connections by `socket.io` clients. In other words, any HTTP server can implement socket.io and still serve other normal, non-realtime HTTP requests.\n\n- *transports*\n\n```js\n['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']\n```\n\n  A list of the transports to attempt to utilize (in order of preference).\n\n- *'connect timeout'*\n\n```js\n5000\n```\n\n  The amount of milliseconds a transport has to create a connection before we consider it timed out.\n  \n- *'try multiple transports'*\n\n```js\ntrue\n```\n\n  A boolean indicating if we should try other transports when the  connectTimeout occurs.\n  \n- *reconnect*\n\n```js\ntrue\n```\n\n  A boolean indicating if we should automatically reconnect if a connection is disconnected. \n  \n- *'reconnection delay'*\n\n```js\n500\n```\n\n  The amount of milliseconds before we try to connect to the server again. We are using a exponential back off algorithm for the following reconnections, on each reconnect attempt this value will get multiplied (500 > 1000 > 2000 > 4000 > 8000).\n  \n\n- *'max reconnection attempts'*\n\n```js\n10\n```\n\n  The amount of attempts should we make using the current transport to connect to the server? After this we will do one final attempt, and re-try with all enabled transport methods before we give up.\n\n##### Properties:\n\n- *options*\n\n  The passed in options combined with the defaults.\n\n- *connected*\n\n  Whether the socket is connected or not.\n  \n- *connecting*\n\n  Whether the socket is connecting or not.\n\n- *reconnecting*\n\n  Whether we are reconnecting or not.\n  \n- *transport*  \n\n  The transport instance.\n\n##### Methods:\n  \n- *connect(λ)*\n\n  Establishes a connection. If λ is supplied as argument, it will be called once the connection is established.\n  \n- *send(message)*\n  \n  A string of data to send.\n  \n- *disconnect*\n\n  Closes the connection.\n  \n- *on(event, λ)*\n\n  Adds a listener for the event *event*.\n\n- *once(event, λ)*\n\n  Adds a one time listener for the event *event*. The listener is removed after the first time the event is fired.\n  \n- *removeListener(event, λ)*\n\n  Removes the listener λ for the event *event*.\n  \n##### Events:\n\n- *connect*\n\n  Fired when the connection is established and the handshake successful.\n  \n- *connecting(transport_type)*\n\n    Fired when a connection is attempted, passing the transport name.\n  \n- *connect_failed*\n\n    Fired when the connection timeout occurs after the last connection attempt.\n  This only fires if the `connectTimeout` option is set.\n  If the `tryTransportsOnConnectTimeout` option is set, this only fires once all\n  possible transports have been tried.\n  \n- *message(message)*\n  \n  Fired when a message arrives from the server\n\n- *close*\n\n  Fired when the connection is closed. Be careful with using this event, as some transports will fire it even under temporary, expected disconnections (such as XHR-Polling).\n  \n- *disconnect*\n\n  Fired when the connection is considered disconnected.\n  \n- *reconnect(transport_type,reconnectionAttempts)*\n\n  Fired when the connection has been re-established. This only fires if the `reconnect` option is set.\n\n- *reconnecting(reconnectionDelay,reconnectionAttempts)*\n\n  Fired when a reconnection is attempted, passing the next delay for the next reconnection.\n\n- *reconnect_failed*\n\n  Fired when all reconnection attempts have failed and we where unsuccessful in reconnecting to the server.  \n\n### Contributors\n\nGuillermo Rauch <guillermo@learnboost.com>\n\nArnout Kazemier <info@3rd-eden.com>\n\n### License \n\n(The MIT License)\n\nCopyright (c) 2010 LearnBoost <dev@learnboost.com>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
  "_id": "socket.io-client@0.9.11",
  "_from": "socket.io-client@0.9.11"
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions