Permalink
Browse files

added streaming support, updated readme

  • Loading branch information...
1 parent b6c1216 commit 0cebf5044f393533471cd3497427577fd1a9f6d9 andris9 committed Nov 23, 2011
Showing with 55 additions and 9 deletions.
  1. +20 −0 README.md
  2. +3 −5 examples/client.js
  3. +13 −0 examples/streamClient.js
  4. +16 −0 examples/streamWorker.js
  5. +2 −3 examples/worker.js
  6. +1 −1 package.json
View
@@ -89,6 +89,26 @@ Example:
worker.end(reversed);
});
+## Streaming
+
+Worker and job objects also act as Stream objects (workers are writable and jobs readable streams), so you can stream data with `pipe` from a worker to a client (but not the other way round).
+
+**Streaming worker**
+
+ gearman.registerWorker("stream_file", function(payload, worker){
+ var input = fs.createReadStream(filepath);
+ // stream file to client
+ input.pipe(worker);
+ });
+
+**Streaming client**
+
+ var job = gearman.submitJob("stream", null),
+ output = fs.createWriteStream(filepath);
+
+ // save incoming stream to file
+ job.pipe(output);
+
## License
**MIT**
View
@@ -1,9 +1,7 @@
-var Gearman = require("./gearman");
+var Gearman = require("../lib/gearman"),
+ gearman = new Gearman("pangalink.net");
-var gearman = new Gearman("pangalink.net");
-
-var y = new Gearman("pangalink.net");
-var job = y.submitJob("reverse", "test");
+var job = gearman.submitJob("reverse", "test");
job.on("error", function(err){
console.log("ERROR: ", err.message || err);
View
@@ -0,0 +1,13 @@
+var Gearman = require("../lib/gearman"),
+ fs = require("fs"),
+ gearman = new Gearman("pangalink.net");
+
+var job = gearman.submitJob("stream", null),
+ output = fs.createWriteStream(__dirname+"/../../wordlist.txt.copy");
+
+job.pipe(output, {end: false});
+
+job.on("end", function(){
+ console.log("transfer ready");
+ gearman.close();
+});
View
@@ -0,0 +1,16 @@
+var Gearman = require("../lib/gearman"),
+ fs = require("fs");
+
+var gearman = new Gearman("pangalink.net",0,1);
+
+var filepath = __dirname+"/../../wordlist.txt";
+
+gearman.registerWorker("stream", function(payload, worker){
+ // pipe stream to worker
+ console.log("start transfer");
+ fs.createReadStream(filepath).pipe(worker);
+});
+
+gearman.on("connect", function(){
+ console.log("Listening for jobs");
+})
View
@@ -1,6 +1,5 @@
-var Gearman = require("./gearman");
-
-var gearman = new Gearman("pangalink.net",0,1);
+var Gearman = require("../lib/gearman"),
+ gearman = new Gearman("pangalink.net");
gearman.registerWorker("reverse", function(payload, worker){
var str = "", i;
View
@@ -1,7 +1,7 @@
{
"name": "node-gearman",
"description": "Simple Gearman client/worker module for Node.JS",
- "version": "0.1.0",
+ "version": "0.1.1",
"author" : "Andris Reinman",
"maintainers":[
{

0 comments on commit 0cebf50

Please sign in to comment.