{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Send and Receive Messages","type":"basic","slug":"send-and-receive-messages","excerpt":"Connect to Octoblu and send/receive messages","body":"Create a file called \"index.js\" with the code below:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var meshblu = require('meshblu');\\n\\n// Not specifying a UUID/Token auto-registers a new device\\nvar conn = meshblu.createConnection({\\n    \\\"server\\\": \\\"meshblu.octoblu.com\\\",\\n    \\\"port\\\": 80\\n});\\n\\nconn.on('notReady', function(data){\\n    console.log('UUID FAILED AUTHENTICATION!');\\n    console.log(data);\\n});\\n\\nconn.on('ready', function(data){\\n    console.log('UUID AUTHENTICATED!');\\n    console.log(data);\\n\\n    setInterval(function(){\\n        console.log(\\\"sending message\\\");\\n        conn.message({\\n            \\\"devices\\\": data.uuid,\\n            \\\"payload\\\": {\\n                \\\"hello\\\":\\\"world\\\"\\n            }\\n        });\\n    },300);\\n\\n    conn.on('message', function(data){\\n        console.log('message received');\\n        console.log(data);\\n    });\\n\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThen run:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"node index.js\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nHere is the example output:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"trying ws://meshblu.octoblu.com:80\\n      UUID AUTHENTICATED!\\n      { api: 'connect',\\n      status: 201,\\n      socketid: 'UMumKQdMAE73j3tXAACr',\\n      uuid: '5ddfa401-3f62-11e4-9968-a5374769756b',\\n      token: 'edech445oyt7f1or0c6p5pugovv4pldi' }\\n      sending message\\n      message received\\n      { devices: '5ddfa401-3f62-11e4-9968-a5374769756b',\\n      payload: { hello: 'world' },\\n      fromUuid: '5ddfa401-3f62-11e4-9968-a5374769756b' }\\n      sending message\\n      message received\\n      { devices: '5ddfa401-3f62-11e4-9968-a5374769756b',\\n      payload: { hello: 'world' },\\n      fromUuid: '5ddfa401-3f62-11e4-9968-a5374769756b' }\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]","updates":["5873517b1ddb870f008bad38"],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"556745c977c15523002fd58a","__v":1,"category":{"sync":{"isSync":false,"url":""},"pages":["556745611c38390d006980a2","556745c977c15523002fd58a","556746391c38390d006980a7","556746831c38390d006980a9"],"title":"Demos","slug":"demos","order":9,"from_sync":false,"reference":false,"_id":"556741d87acd550d0075eacb","project":"5564f26a1fd04c0d00dc9aaa","version":"5564f26a1fd04c0d00dc9aad","__v":4,"createdAt":"2015-05-28T16:27:04.708Z"},"createdAt":"2015-05-28T16:43:53.971Z","githubsync":"","parentDoc":null,"project":"5564f26a1fd04c0d00dc9aaa","user":"5564f227f0f70f0d00a9ab20","version":{"version":"1.0","version_clean":"1.0.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["5564f26b1fd04c0d00dc9aae","556741d17acd550d0075eaca","556741d87acd550d0075eacb","556742f87acd550d0075ead1","556781cd6976ef0d0099c545","5568d666d33aad0d00ec8d2e","557f6b2d38249b0d00d0d12b","55b67be9c2e909190073ed38","564e56c601e80e0d00396684","56731b714b2a680d00524daf","573612ac652bd80e00a90027","57365e2cf8ebd31700769f50","57366131f8ebd31700769f58","573b93514e029d19000b8669","573bbfdb7ac6f6170033bd35"],"_id":"5564f26a1fd04c0d00dc9aad","createdAt":"2015-05-26T22:23:38.671Z","project":"5564f26a1fd04c0d00dc9aaa","releaseDate":"2015-05-26T22:23:38.671Z","__v":15}}

Send and Receive Messages

Connect to Octoblu and send/receive messages

Create a file called "index.js" with the code below: [block:code] { "codes": [ { "code": "var meshblu = require('meshblu');\n\n// Not specifying a UUID/Token auto-registers a new device\nvar conn = meshblu.createConnection({\n \"server\": \"meshblu.octoblu.com\",\n \"port\": 80\n});\n\nconn.on('notReady', function(data){\n console.log('UUID FAILED AUTHENTICATION!');\n console.log(data);\n});\n\nconn.on('ready', function(data){\n console.log('UUID AUTHENTICATED!');\n console.log(data);\n\n setInterval(function(){\n console.log(\"sending message\");\n conn.message({\n \"devices\": data.uuid,\n \"payload\": {\n \"hello\":\"world\"\n }\n });\n },300);\n\n conn.on('message', function(data){\n console.log('message received');\n console.log(data);\n });\n\n});", "language": "javascript" } ] } [/block] Then run: [block:code] { "codes": [ { "code": "node index.js", "language": "text" } ] } [/block] Here is the example output: [block:code] { "codes": [ { "code": "trying ws://meshblu.octoblu.com:80\n UUID AUTHENTICATED!\n { api: 'connect',\n status: 201,\n socketid: 'UMumKQdMAE73j3tXAACr',\n uuid: '5ddfa401-3f62-11e4-9968-a5374769756b',\n token: 'edech445oyt7f1or0c6p5pugovv4pldi' }\n sending message\n message received\n { devices: '5ddfa401-3f62-11e4-9968-a5374769756b',\n payload: { hello: 'world' },\n fromUuid: '5ddfa401-3f62-11e4-9968-a5374769756b' }\n sending message\n message received\n { devices: '5ddfa401-3f62-11e4-9968-a5374769756b',\n payload: { hello: 'world' },\n fromUuid: '5ddfa401-3f62-11e4-9968-a5374769756b' }", "language": "text" } ] } [/block]