Stream WebM file to Chrome using Node.js


Node.js can be used to stream arbitrary data to a browser such as Chrome, over HTTP. In this post we’ll use latest version of the express middleware to stream a WebM file to the browser.

Install express

Execute the following npm command to install express

sudo npm install express@latest

npm installs express to a folder called node_modules, under the current folder. If you run node in the current folder, it should be able to find express.

Code

Create a a file called webm.js with the following code

The commented headers in the response may be used for additional control. The Transfer-Encoding header may also be identity, its default value, as long as the Connection response header is close. If Connection header is keep-alive, Transfer-Encoding has to be chunked. This behavior may be browser specific, I have only tested with Chrome. Chunking is taken care of by Node.js.

Running the code

To stream a WebM file at /home/user/file.webm invoke node like

node webm.js 9001 /home/user/file.webm

Then, point Chrome to http://host:9001/, and the video should begin playing.

Doing it the easy way

Now that we have seen the hard way, express has a method on the response object to send a file. It basically is a replacement for all the code that exists in the app.get() callback above:

  res.sendfile(file);
Advertisements

3 thoughts on “Stream WebM file to Chrome using Node.js

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s