Cloudinary Blog

Building a Camera App With WebRTC, Vue.js, and Cloudinary

Create a Camera App With WebRTC, Vue.js, and Cloudinary

Since 1999, WebRTC has served as an open-source and free solution for real-time audio and video communications within webpages, eliminating the need to use plugins, native apps, or other third-party software. I first became aware of WebRTC when Google and other browser vendors started supporting it. Many products, including Amazon Chime and Slack, have since jumped on the bandwagon.


One notable WebRTC capability is that its API enables you to take pictures with the camera on the device that’s rendering your webpage. In my new role as a developer instructional designer for Cloudinary, I recently built an instructional app, called Vue Camera, that integrates with our upload and delivery services and that can take pictures for upload to my Cloudinary account. This article describes the related components and process.

How to Optimize for Page Load Speed

Vue Camera’s Components

Vue Camera contains two routes:

  • The home route, called Camera, has a component that takes pictures.
  • The Gallery route displays pictures. This route and the Upload feature are disabled until after you’ve specified a cloud name and an unsigned upload preset.

Vue Camera is responsive and works well on desktop or mobile. Even though it can take pictures that face away from the device, its ability to do so depends on the device on which it’s running. For example, I can only take selfies on my MacBook Pro but can take both selfies and outward pictures on my Google Pixel. On its home page, WebRTC states that it does not support all browsers.

The App Framework

The name Vue Camera clues you in on the JavaScript framework for building the app: Vue.js. Additionally, I used many APIs:

  • WebRTC for streaming image data from a device camera
  • Canvas toDataURL for representing formatted images as data
  • Vue.js plugins:
    • Vue-bootstrap for responsive CSS
    • Vuex, an implementation of the Flux container architecture
    • Vue-ls for HTML5 local storage
    • Vue-router for creating routable views
  • Cloudinary’s Upload API for uploading images to a Cloudinary account
  • Cloudinary’s Product Gallery JavaScript function for rendering a responsive display of images in a Cloudinary account

For details, see the complete code on Github.

The Part Played by the APIs

Let’s take a high-level look at how the APIs work together in Vue.js to create Vue Camera. Like many similar apps, Vue Camera performs these tasks:

  • Collects data.
  • Transmits the data to a database or static storage.
  • Displays the data to users in another format.

WebRTC: Capture Data

The WebRTC APIs are asynchronous, promise-based browser calls that discover the source of the data and then capture it from the device they run on. Those APIs are wrapped in a component in Vue Camera.

Cloudinary Upload API: Save Data

Cloudinary offers an upload endpoint that can be requested by many HTTP clients, including Axios, XMLHttpRequest (XHR), jQuery, and Angular’s HttpClient. Depending on the HTTP client, the code handles a callback or a promise. Axios posts data to Cloudinary in the Camera component.

Cloudinary Product Gallery: Display Data

A JavaScript function in Cloudinary renders a responsive set of images. Even though most frameworks work with plugins or npm libraries, this code wraps a JavaScript function call that binds to a Document Object Model (DOM) element from within a framework lifecycle. That way, you can take advantage of code that might not otherwise be readily available to the framework.

You can build apps in the React and Angular frameworks with the same technique. As you move between app frameworks like Vue.js, Angular, and React, you organize, configure, and deliver captured data and render HTML, as appropriate. The frameworks have in common similar integrations with the outside world, following a component-based structure for modular development and ensuring that components are bound to the DOM. The idea is that you as the developer are creating HTML tags with your components, which is why you can integrate API functionalities in other frameworks just as you do with this Vue.js app.

Setup on Cloudinary

Now let’s look at how Vue Camera works. To run the app from the demo site, sign up for a free Cloudinary account and then create an unsigned upload preset to upload your pictures to Cloudinary and view them in the Gallery. Click the info icon (see the screenshot below) for details.


Here are the steps:

  1. Click the gear icon at the top of the Cloudinary console for the settings.
  2. Click Upload below the top navigation.
  3. Under Upload presets, click Enable unsigned uploads and then click Add upload preset.
  4. Under Upload preset name, type a name for the preset, e.g., my-unsigned-preset.
  5. Under Signing Mode, choose Unsigned from the drop-down menu.
  6. Click Save at the top.

Cloudinary then displays the preset you just created under Upload presets in the next screen.

unsigned upload preset

Now open Vue Camera and register your cloud name and the preset. Click the gear icon for the Cloudinary Upload Info form and fill in the two text fields with your cloud name and preset name. Click OK.


Afterwards, Cloudinary displays the Upload button with the Gallery link enabled.

Important: The first time you run Vue Camera on a device, you must grant permission for the app to use the camera.

You can now click Snapshot to take pictures and Upload to post them to your Cloudinary account.

What do your cloud name and preset do behind the scenes? Cloudinary loads them into the Vuex store, making them available for use by all components. Cloudinary also stores them in the HTML5 local storage for one hour, saving you having to reregister them if you leave your browser and come back within that time.

local storage

In essence, the cloud name and preset act as credentials in Vue Camera, providing the information Cloudinary needs for its Upload API to post data. Note that you don’t need credentials to download images to your local drive with Vue Camera. However, to save them to the cloud, you must register your cloud name and preset.

Implementation on WebRTC

The WebRTC code resides in the camera component in a model that includes media, devices, and constraints. The media is in the form of a data stream. The devices are detected and then opened with constraints, thus enabling the flow of media. See the snippet below.

Copy to clipboard
getDevices: async function() {
     if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
       return false;
     try {
       let allDevices = await navigator.mediaDevices.enumerateDevices();
       for (let mediaDevice of allDevices) {
         if (mediaDevice.kind === "videoinput") {
           let option = {};
           option.text = mediaDevice.label;
           option.value = mediaDevice.deviceId;
       return true;
     } catch (err) {
       throw err;

The HTML code loads with an HTML5 video tag and a hidden canvas tag. WebRTC directs the video stream to the video tag so that when the user clicks Snapshot, WebRTC reads the video frame as a JPEG data URI, ready for handoff to the Cloudinary Upload API. See the snippet below.

Copy to clipboard
 snapshot: function() {
     this.canvas.width =;
     this.canvas.height =;
       .drawImage(, 0, 0, this.canvas.width, this.canvas.height);
     this.fileData = this.canvas.toDataURL("image/jpeg");
     this.isPhoto = true;
     this.cameraState = false;
     //remove any hidden links used for download
     let hiddenLinks = document.querySelectorAll(".hidden_links");
     for (let hiddenLink of hiddenLinks) {

Cloudinary Upload API

The uploadToCloudinary function packages the registered cloud name and preset with the file data obtained in the snapshot described above as a FormData object. I posted the data to Cloudinary with the Axios library so the cloud name becomes part of the upload endpoint.

You can organize media on Cloudinary in many ways. One of them is through tags, which, like metadata, can help you identify your assets. To take advantage of that capability, I added a tag named browser_upload, with which the Gallery component pulls only the pictures taken by Vue Camera. See the snippet below.

Copy to clipboard
async function uploadToCloudinary(cloudName, preset, fileData) {
 try {
   let fd = new FormData();
   let url = `${cloudName}/image/upload`;
   fd.append("upload_preset", preset);
   fd.append("tags", "browser_upload");
   fd.append("file", fileData);
   let res = await axios({
     method: "post",
     url: url,
     data: fd
   return await;
 } catch (err) {
   throw err;

If the upload succeeds, the function that calls uploadToCloudinary notifies the user with a “toast” message. The code below shows such a message that’s five seconds long.

Copy to clipboard
         `Upload to Cloudinary unsuccessful: use settings to provide cloudname and preset`,
           title: "Cloudinary Upload",
           autoHideDelay: 5000,
           appendToast: false

If you’ve entered a nonexistent cloud name or preset, a fail error-message is displayed.


Now comes the step to show off the images. Enter Cloudinary’s Product Gallery widget, available as a JavaScript function that binds output to a DOM element, much like what you would do in jQuery or Vanilla JavaScript. So, how to include that function in a framework?

The Gallery component code shows how I “wrapped” the Gallery function in a component's script with a Vue.js lifecycle hook. First, when the component elements are added to the virtual DOM, Vue.js calls the mounted function, gets an instance of the Product Gallery widget, and renders the Product Gallery output to the DOM. Finally, to prevent memory leaks, the function called by the beforeDestroy lifecycle hook destroys the gallery element when exiting the component view. See the snippet below.

Copy to clipboard
mounted() {
   //get cloudname and preset from local storage
   if (this.$ls.get("cloudname")) {
     this.cloudname = this.$ls.get("cloudname");
   if (this.$ls.get("preset")) {
     this.preset = this.$ls.get("preset");
   //if these aren't set don't allow browse
   /*global cloudinary*/
   /*eslint no-undef: "error"*/
   this.myGallery = cloudinary.galleryWidget({
     container: "#images",
     cloudName: this.cloudname,
     mediaAssets: [{ tag: "browser_upload" }]

Be sure to add links for the widget CSS and JavaScript to your public/index.html file, which is not part of the webpacked code.

Copy to clipboard
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width,initial-scale=1.0">
 <link rel="icon" href="<%= BASE_URL %>favicon.ico">
 <link href="//,400,500,700" rel="stylesheet" type="text/css">
 <link rel="stylesheet" href="" integrity="sha384-KA6wR/X5RY4zFAHpv/CnoG2UW1uogYfdnP67Uv7eULvTveboZJg0qUpmJZb5VqzN" crossorigin="anonymous">
 <link rel="stylesheet" href="./css/main.css">


   <strong>We're sorry but vue-camera doesn't work properly without JavaScript enabled. Please enable it to
 <div id="app"></div>
 <script src=""></script>
 <!-- built files will be auto injected -->

The Product Gallery widget then becomes responsive, as shown in the screenshot below the desktop view.


The image below shows the mobile view along with a carousel for maintaining a responsive layout. mobile gallery


The Vue Camera app serves as instructional code only and is not intended to replace Google Photos or Mac’s Photo Booth. You can add more capabilities, e.g., device constraints that change the size of the video-stream frame and a feature that shares the Cloudinary link. You can also use the Cloudinary Vue.js SDK plugin to transform images.

If you are running Vue Camera on both desktop and mobile, feel free to use it as a tool for self-reflection. The image below shows me taking a selfie in the desktop version with the mobile version.

laptop As a side note, I presented the material from this article at the API Meetup in Seattle, Washington in November 2019. Gratifyingly, it elicited a lot of interest from the audience.

About Cloudinary

Cloudinary provides easy-to-use, cloud-based media management solutions for the world’s top brands. With offices in the US, UK and Israel, Cloudinary has quickly become the de facto solution used by developers and marketers at major companies around the world to streamline rich media management and deliver optimal end-user experiences.

For more information, visit or follow us on Twitter

Recent Blog Posts

Why the Future of E-commerce Is Live

In a previous post, I discussed how “going live” is gaining popularity across industries and verticals. What began as a way for gamers to jam together has evolved into a medium for broader entertainment and business purposes. To continue the conversation, this post unpacks the current trends of shoppable live streams to shine a light on how brands are leveraging “lives” to connect with shoppers in new ways.

Read more
An Overview of Live-Streaming Video Trends

“Let’s go live.” For decades, that’s what newscasters say as they cut to real-time footage of a colleague reporting in the field. The live-video feed adds visual interest and perspective to a story beyond what can be communicated by someone sitting behind the news desk. In the same way, live-streaming video nowadays adds context to other consumer environments. From gaming and events to shopping and social media, “going live” enhances everyday experiences, and it’s something anyone can do with relative ease.

Read more
Readying Live Streams for Video on Demand

When planning a live broadcast or stream, companies often overlook the redistribution phase, but live-stream videos are useful well beyond their initial streaming. Why? Because not everyone watches the first run. For a wider audience, it makes sense to repost live content on your website under an “events” tab, on YouTube, and other social sites for video on demand (VOD). However, preparing footage for reposting can be a lot of work.

Read more
Optimize Visual Media for a Fast and Captivating Digital Experience

Did you know that humans process imagery 60,000 times faster than text? In fact, 90% of the information our brains process is visual, which makes it seem a no-brainer—pun intended—that brands are connecting with consumers through visual content online. However, adding media assets like images and videos to websites comes with a tradeoff: the more media, the heavier the site, which results in a noticeable slowdown in page loads and a reduction in content quality.

Read more