Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Images, realtime, and RethinkDB

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Images, realtime, and RethinkDB

An introduction of using binary data and RethinkDB changefeeds to create a realtime photo drag and drop app.

Avatar for Jorge Silva

Jorge Silva

March 31, 2015
Tweet

More Decks by Jorge Silva

Other Decks in Technology

Transcript

  1. el.addEventListener(“drop”, function(evt) { // Get the first file only var

    file = evt.dataTransfer.files[0]; var data = new FormData(); data.append(‘file’, file); data.append(‘fileName’, file.name); data.append(‘type’, file.type); // Send an HTTP POST request using the jQuery $.post(‘/image’, data); }, false);
  2. el.addEventListener(“drop”, function(evt) { // Get the first file only var

    file = evt.dataTransfer.files[0]; var data = new FormData(); data.append(‘file’, file); data.append(‘fileName’, file.name); data.append(‘type’, file.type); // Send an HTTP POST request using the jQuery $.post(‘/image’, data); }, false);
  3. el.addEventListener(“drop”, function(evt) { // Get the first file only var

    file = evt.dataTransfer.files[0]; var data = new FormData(); data.append(‘file’, file); data.append(‘fileName’, file.name); data.append(‘type’, file.type); // Send an HTTP POST request using the jQuery $.post(‘/image’, data); }, false);
  4. el.addEventListener(“drop”, function(evt) { // Get the first file only var

    file = evt.dataTransfer.files[0]; var data = new FormData(); data.append(‘file’, file); data.append(‘fileName’, file.name); data.append(‘type’, file.type); // Send an HTTP POST request using the jQuery $.post(‘/image’, data); }, false);
  5. app.post(‘/image’, function (req, res) { var form = new multiparty.Form();

    form.parse(req, function (err, fields, files) { var imagePath = files.file[0].path; fs.readFile(imagePath, function (err, buff) { var image = { fileName: fields.fileName[0], type: fields.type[0], file: buff }; r .table(‘images’) .insert(image) .run(r.conn) .then(function () { }); }); }); });
  6. app.post(‘/image’, function (req, res) { var form = new multiparty.Form();

    form.parse(req, function (err, fields, files) { var imagePath = files.file[0].path; fs.readFile(imagePath, function (err, buff) { var image = { fileName: fields.fileName[0], type: fields.type[0], file: buff }; r .table(‘images’) .insert(image) .run(r.conn) .then(function () { }); }); }); });
  7. app.post(‘/image’, function (req, res) { var form = new multiparty.Form();

    form.parse(req, function (err, fields, files) { var imagePath = files.file[0].path; fs.readFile(imagePath, function (err, buff) { var image = { fileName: fields.fileName[0], type: fields.type[0], file: buff }; r .table(‘images’) .insert(image) .run(r.conn) .then(function () { }); }); }); });
  8. app.post(‘/image’, function (req, res) { var form = new multiparty.Form();

    form.parse(req, function (err, fields, files) { var imagePath = files.file[0].path; fs.readFile(imagePath, function (err, buff) { var image = { fileName: fields.fileName[0], type: fields.type[0], file: buff }; r .table(‘images’) .insert(image) .run(r.conn) .then(function () { }); }); }); });
  9. app.post(‘/image’, function (req, res) { var form = new multiparty.Form();

    form.parse(req, function (err, fields, files) { var imagePath = files.file[0].path; fs.readFile(imagePath, function (err, buf) { var image = { fileName: fields.fileName[0], type: fields.type[0], file: buf }; r .table(‘images’) .insert(image) .run(r.conn) .then(function () { }); }); }); }); app.post(‘/image’, function (req, res) { var form = new multiparty.Form(); form.parse(req, function (err, fields, files) { var imagePath = files.file[0].path; fs.readFile(imagePath, function (err, buff) { var image = { fileName: fields.fileName[0], type: fields.type[0], file: buff }; r .table(‘images’) .insert(image) .run(r.conn) .then(function () { }); }); }); });
  10. r .table(‘images’) .changes() .run(conn) .then(function (cursor) { cursor.each(function (err, photo)

    { // Push images through the socket connection if (photo.new_val !== null) { io.emit(‘Image:update’, photo.new_val); } }); });
  11. r .table(‘images’) .changes() .run(conn) .then(function (cursor) { cursor.each(function (err, photo)

    { // Push images through the socket connection if (photo.new_val !== null) { io.emit(‘Image:update’, photo.new_val); } }); });
  12. r .table(‘images’) .changes() .run(conn) .then(function (cursor) { cursor.each(function (err, photo)

    { // Push images through the socket connection if (photo.new_val !== null) { io.emit(‘Image:update’, photo.new_val); } }); });
  13. r .table(‘images’) .changes() .run(conn) .then(function (cursor) { cursor.each(function (err, photo)

    { // Push images through the socket connection if (photo.new_val !== null) { io.emit(‘Image:update’, photo.new_val); } }); });
  14. var socket = io.connect(‘http://localhost:8000’); socket.on(‘Image:update’, function (image) { var reader

    = new FileReader(); reader.onload = function(e) { $(‘#images’) .html( ‘<img ’ + ‘src=”’ + e.target.result + ‘” />’ ); }.bind(this); reader.readAsDataURL(new Blob([image.file])); });
  15. var socket = io.connect(‘http://localhost:8000’); socket.on(‘Image:update’, function (image) { var reader

    = new FileReader(); reader.onload = function(e) { $(‘#images’) .html( ‘<img ’ + ‘src=”’ + e.target.result + ‘” />’ ); }; reader.readAsDataURL(new Blob([image.file])); });
  16. var socket = io.connect(‘http://localhost:8000’); socket.on(‘Image:update’, function (image) { var reader

    = new FileReader(); reader.onload = function(e) { $(‘#images’) .html( ‘<img ’ + ‘src=”’ + e.target.result + ‘” />’ ); }.bind(this); reader.readAsDataURL(new Blob([image.file])); }); var socket = io.connect(‘http://localhost:8000’); socket.on(‘Image:update’, function (image) { var reader = new FileReader(); reader.onload = function(e) { $(‘#images’) .html( ‘<img ’ + ‘src=”’ + e.target.result + ‘” />’ ); }.bind(this); reader.readAsDataURL(new Blob([image.file])); });
  17. Thank You Questions? rethinkdb.com / blog.hiphipjorge.com @rethinkdb / @thejsj Thank

    You Questions? github.com/thejsj/realtime-photo github.com/thejsj/realtime-photo-tutorial rethinkdb.com / blog.hiphipjorge.com @rethinkdb / @thejsj