Streaming files to MongoDB using Socket.io and GridFS

Today I discovered how easy it is to send a file over the web sockets directly to MongoDB GridFS.
It is possible thanks to two amazing node modules. One of them is Naoyuki Kanezawa's Socket.IO stream. The other one is Aaron Heckmann's gridfs-stream.

I will get right to the point. I will just assume you read the documentation of both modules, and included them into your Node.js project. The client side code will be no different then the example in Socket.IO stream project documentation. This is example how receiving of the file and writing it to GridFS could look like:

io.sockets.on('connection', function (socket) {
    ss(socket).on('uploadFile', function (stream, data, callback) {
        var writestream = gfs.createWriteStream();
        stream.pipe(writestream);
    });  
    ...
});

It is not too complicated, because those two modules work perfectly together. If you use MongoDB and Socket.io in your project, I would recommend to try out this method of uploading and storing data.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近这几天,包括亿欧网、亿邦动力网等科技行业内的各大垂直门户网站分别报道了有关于“老子云”4月25日的发布会,而关...
    手撕牛魔王阅读 402评论 0 0
  • 以前经常听到说:“跟什么样的人在一起你就是什么样的人”。现在这句话依然受用,只是我自己把它改了一点点“跟什么样的...
    __感悟阅读 351评论 0 1
  • 从我们在一起的那天我们开始约法三章,为了你自己,为了我,为了我们无悔的青春 第一章 你要爱你自己 我明白你对我的喜...
    张张丶阅读 322评论 0 3
  • Day08 细节描述:一寸窗户-键盘 这一寸窗户是电脑键盘的一角,由小红点、黑色键盘方块和黑色的空隙构成。 其中小...
    笑笑之阅读 161评论 0 0
  • 我参与了“来简书聊聊你的产品之路|@产品专题征文”,也来说说你的产品故事吧。 从2017年4月进入产品工作实习到2...
    小明说1993阅读 307评论 1 5