{"__v":2,"_id":"54dd28c8db3b362100131296","category":{"__v":19,"_id":"54d5635632d98b0d00384afe","pages":["54d5635732d98b0d00384b12","54d5635732d98b0d00384b13","54d5635732d98b0d00384b14","54d847c125e90a0d00db551f","54d995416a09052100a6b6d7","54d995534c49f40d00dff98a","54d9956b4c49f40d00dff98c","54dbce210e553a0d00dc1d09","54dcdf7bac05560d00335a15","54dd133c08f6b61700a691f7","54dd136108f6b61700a691fa","54dd27e0db3b362100131294","54dd2838ca1e5219007e9dcb","54dd28c8db3b362100131296","54deb1133a91410d001b1791","54deb134c4a3570d009d4a60","54dec5c0c2b4b70d009c3f05","54ded495e8e35a0d007551a0","54ded57d8e7b000d007c7f31","551447a90edb420d0050016e","551c4369ff892b3700d1c616"],"project":"54d53c7b23010a0d001aca0c","version":"54d5635532d98b0d00384afb","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-02-06T23:44:18.708Z","from_sync":false,"order":4,"slug":"api-plugins","title":"API Plugins"},"parentDoc":null,"project":"54d53c7b23010a0d001aca0c","user":"54cfa8e1a8a4fd0d00b7fd1d","version":{"__v":10,"_id":"54d5635532d98b0d00384afb","forked_from":"54d53c7c23010a0d001aca0f","project":"54d53c7b23010a0d001aca0c","createdAt":"2015-02-07T00:59:01.934Z","releaseDate":"2015-02-07T00:59:01.934Z","categories":["54d5635632d98b0d00384afc","54d5635632d98b0d00384afd","54d5635632d98b0d00384afe","54d5635632d98b0d00384aff","54d5635632d98b0d00384b00","54d5635632d98b0d00384b01","54d5635632d98b0d00384b02","54d652097e05890d006f153e","54dd1315ca1e5219007e9daa","54e21e2b22de1c230094b147","54e68e62a43fe13500db3879","54fa1d3fe7a0ba2f00306309","551c453a23a1ee190034d19a","551df586e52a0b23000c62b6","551f39be6886f8230055f02a","55a6720751457325000e4d97"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"0.1.0","version":"0.1"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-02-12T22:27:20.098Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"Plugins often want to interact with files, processes and network within the user's workspace. Because all Cloud9 plugins run in the browser and there is a network between the browser and the user's workspace we looked for an API that would work asynchronously. The Node.js APIs were an obvious match and so we based our Filesystem (`fs`), Process (`proc`) and Network (`net`) APIs on Node's fs, child_process and net modules.\n\n# Getting a reference to the plugin\n\nThe following examples show how to gain access to the `net` APIs. \n\nFirst, make sure to add \"net\" to your list of plugins you depend on like so:\n\n```\nmain.consumes = [\"Plugin\", \"net\"];\n```\n\nAnd then get a reference to the plugin in the main function:\n\n```\nfunction main(options, imports, register) {\n    var net = imports.net;\n```\n\n# Network\n\n*For a complete reference to all the methods and events of the `net` plugin check out the [net api reference](https://docs.c9.io/api/#!/api/net).*\n\nThe following table lists of all the methods supported by the `net` plugin. \n[block:html]\n{\n  \"html\": \"<table>\\n  <tr><th>connect</th><td>Connects a regular socket to a port in the workspace.</td></tr>\\n</table>\"\n}\n[/block]\nThe following example show the most common use cases connecting to a port on the 'local' workspace machine.\n\nConnecting to a locally running HTTP server and getting the index page\n```\nnet.connect(8080, {}, function(err, stream) {\n    if (err) return console.error(err);\n\n    stream.on(\"data\", function(chunk) {\n        console.log(chunk);\n    });\n    stream.write(\"GET /\");\n});\n```","excerpt":"","slug":"the-network-api","type":"basic","title":"The Network API"}
Plugins often want to interact with files, processes and network within the user's workspace. Because all Cloud9 plugins run in the browser and there is a network between the browser and the user's workspace we looked for an API that would work asynchronously. The Node.js APIs were an obvious match and so we based our Filesystem (`fs`), Process (`proc`) and Network (`net`) APIs on Node's fs, child_process and net modules. # Getting a reference to the plugin The following examples show how to gain access to the `net` APIs. First, make sure to add "net" to your list of plugins you depend on like so: ``` main.consumes = ["Plugin", "net"]; ``` And then get a reference to the plugin in the main function: ``` function main(options, imports, register) { var net = imports.net; ``` # Network *For a complete reference to all the methods and events of the `net` plugin check out the [net api reference](https://docs.c9.io/api/#!/api/net).* The following table lists of all the methods supported by the `net` plugin. [block:html] { "html": "<table>\n <tr><th>connect</th><td>Connects a regular socket to a port in the workspace.</td></tr>\n</table>" } [/block] The following example show the most common use cases connecting to a port on the 'local' workspace machine. Connecting to a locally running HTTP server and getting the index page ``` net.connect(8080, {}, function(err, stream) { if (err) return console.error(err); stream.on("data", function(chunk) { console.log(chunk); }); stream.write("GET /"); }); ```