Skip to content

Instantly share code, notes, and snippets.

@mmaelzer
Created March 5, 2015 21:48
Show Gist options
  • Save mmaelzer/39bc2cacaf5f3ee50a23 to your computer and use it in GitHub Desktop.
Save mmaelzer/39bc2cacaf5f3ee50a23 to your computer and use it in GitHub Desktop.
mjpeg-camera + socket.io
var MjpegCamera = require('mjpeg-camera');
var WriteStream = require('stream').Writable;
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var camera = new MjpegCamera({
user: 'username',
password: 'password',
url: 'http://camera-ip-address',
name: 'camera'
});
camera.start();
var ws;
io.on('connection', function(socket) {
ws = new WriteStream({objectMode: true});
s._write = function(chunk, enc, next) {
var jpeg = chunk.data.toString('base64');
socket.emit('frame', {data: jpeg});
next();
};
camera.pipe(ws);
});
io.on('disconnect', function() {
camera.unpipe(ws);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment