Transmitting sound information (encoded in base64) through $resource

Currently, I am developing an application that requires recording audio using a microphone and transmitting it to a backend server (tomcat). However, I have encountered an issue where sending large streams causes Angular to become unresponsive and freezes the browser.

To capture the audio file, I utilize the native function RecorderWorkerFactory.getUserMedia() to obtain a RecordBlob object in Angular. Subsequently, I extract the audio data into base64 encoding and send it to the backend server using $resource. The backend successfully receives and processes the data, but Firefox detects an infinite loop during the callback execution, resulting in a freeze.

Despite this issue, if the program continues to run, the page refresh eventually proceeds after a long delay.


The following code snippet demonstrates how I extract and send the audio content in base64 format:

var blob = $scope.audio.recordBlob;
if (blob) {
    var reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.onloadend = function() {
        $scope.audioContent = reader.result;
        $scope.sendMessage();
    }
}

$scope.sendMessage = function(){
    var outputStream = {
                      "audio": $scope.audioContent
                     };
    $scope.sendIM(outputStream);
}

I send the outputStream via POST request to the backend and trigger the loadData() function in the callback to reload the view.

services.FileCreation= $resource(URI_SERVICE_CREATION, {}, {
    'post' : urlEncodedFormPost
});


$scope.sendIM = function(fluxSortie) {
  $services.FileCreation.post(angular.toJson(outputStream)).$promise.then(function(result){ 
        $scope.loadData();
    });
}

Additionally, here is the Java code for creating the audio file:

private void createAudioFile(File file, byte[] content) throws IOException {
    FileOutputStream stream = null;
    try {
        stream = new FileOutputStream(file.getPath());
        IOUtils.write(content, stream);
    } catch (IOException e) {
        LOGGER.debug("creation failed");
    } finally {
        if (stream != null) {
            stream.flush();
            stream.close();
        }
    }
}

The content variable contains the conversion of the sent base64 string.


After investigating, I discovered that the infinite loop occurs in a native Angular function called shallowClearAndCopy(), which triggers after the Java execution but before the callback. This function seemingly converts each character of the base64-encoded audio string into an object property and loops through them for deletion. Consequently, Firefox interprets this as an infinite loop.

function shallowClearAndCopy(src, dst) {
  dst = dst || {};

  angular.forEach(dst, function(value, key) { // The freezing point, iterating over all characters of the base64 encoded data
    delete dst[key];
  });

  for (var key in src) {
    if (src.hasOwnProperty(key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) {
      dst[key] = src[key];
    }
  }

  return dst;
}

Is this performance issue related to AngularJS, or is there a solution I might be overlooking? Could there be an error in my callback setup?

Thanks for any insights!

Answer №1

After thorough investigation, I finally unearthed the root cause!

The culprit turned out to be the angular method.toJson(outputStream) which unnecessarily altered the object.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to utilize a parameter for the user's input instead of relying on document.getElementById when incorporating a button?

let totalScore = 0; var myArray = [1, 2, 3, 4, 5]; let randomNum; function NumGuess(userInput) { var userGuess = userInput; var i; for (i = 0; i < 1; i++) { var randomNum = myArray[Math.floor(Math.random() * myArray.length)]; } if (us ...

Challenge with Redrawing the w2ui Grid

Using w2ui grid (1.4) with angular 1.3.4, I am attempting to display the grid in one view successfully upon first load. However, when I change the view, the grid fails to load and throws an error. ERROR: The parameter "name" is not unique. There are other ...

Exploring the power of internal linking with GatsbyJS

I am currently developing a website using gatsbyjs. I have concerns about the crawlability of "onClick" for SEO purposes and I would appreciate some assistance. This is my current code: render={data => ( <div className='feed'> ...

What could be the reason for meteor not injecting the text from my template helpers?

My goal is to dynamically generate a table displaying two different sets of data. Despite having verified returns from my database, the rendered page does not show the corresponding HTML elements as expected. Here is the template and HTML code: <templ ...

Should we consider using a boolean-returning function for ng-show?

Will continuously watching a function's value or using it in a directive like ng-show have a performance impact due to the function being called each cycle? Could it be more efficient to simply use a boolean value to determine this, and update that v ...

Identifying when the mouse hovers over a hidden element

Is there a way to make events trigger for <mark> elements under a <textarea>? I've tried adding mouseover listeners, but they don't seem to work because of the <textarea>. I need the <text-area> to function normally, so u ...

Retrieving currentUser data using Vue.js, Vuex, Express, and MongoDB

I am currently working on creating a MEVN stack authentication page that displays information about the user who is logged in. After successfully logging in, the router redirects to Home.vue and passes the username as a prop. The onSubmit method in Login. ...

I am seeking assistance with incorporating mySQL into my Node.js project with React

Currently, I am working on a web-app utilizing Node.js. The main goal is to retrieve information from my local database in MySQL Workbench. Initially, I decided to focus on building the frontend interface before diving into implementing the functionality s ...

Inform registered customers by utilizing AngularJS (angular-websocket-service) and Spring Boot to implement Websockets notifications

Exploring the world of AngularJS and FullStack development is an exciting journey for me. The architectural setup of my current app is already in place and ideally should not be altered (for security reasons). I've been able to send messages to the se ...

What are the methods used by Spring security to manage http basic authentication?

I have implemented RESTful webservices and the first form requires user login with credentials sent in Authorization header like this: Authorization :Basic adajajffjfksal In my security-context.xml file, I have secured the URL as follows: <http patte ...

Can a TextView lose its flexibility when placed within a Toolbar?

Recently, I attempted to follow a tutorial (https://guides.codepath.com/android/using-the-app-toolbar#custom-title-view) on creating a custom TextView inside a Toolbar. My goal was to be able to dynamically change the text inside the TextView using Java. H ...

Adjust a Specific CSV Value Using JavaScript and NodeJS

Hey there, I have a CSV file that I need to extract and modify a specific value from. Let's take this as an example: CSV 90,90,90,90,90 3,1,1000,2,931 I'm looking to access the number "2" in this CSV. How would I go about doing that? And once ...

When attempting to submit a Flink job, encountering issues with incompatible class versions

I encountered an issue while attempting to run a streaming job with Beam 2.27/Flink 1.12 using Maven. Initially, everything was running smoothly until I made some changes and tried to execute the updated pipeline version, which resulted in the following er ...

When the menu active item is clicked, the header will automatically scroll to the top

I've implemented a sticky header using CSS and JavaScript along with a mega menu. However, I've encountered an issue where when scrolling to the bottom and clicking on the "more" link in the dropdown, the page scrolls back to the top. This is the ...

Retrieve MongoDB collection using Node.js

I am completely new to the express/mongo stack, and I have encountered an issue that I was unable to find a solution for on Stack Overflow. Here is my problem: Within my index.js file, the code looks something like this: var mongoose = require('mong ...

What is the method for obtaining a unique exception message in the ajaxError event of jQuery?

Is there a way to customize the content of responseText in my APP? Currently, it displays as exception description amended by Tomcat (refer to the image below). I want to have control over the content of responseText or handle custom messages in a more eff ...

When using the setTimeout function, jQuery's .submit() method may fail to pass data to PHP

I'm attempting to enhance the login form with an effect that triggers a 1-second delay before proceeding to PHP. I tried implementing a setTimeout() function within the .submit event handler along with e.preventDefault() to create the delay, but enco ...

Error: The current element cannot be clicked. Please try again

I have been attempting to trigger a click event on another button within an onClick event function. An error occurred: Uncaught TypeError: this.clickBack.current.click is not a function The React version being used is 16.8.6 constructor(props) { s ...

Dynamic management of Spring Boot MongoDB Repositories across numerous databases

I was faced with the task of updating my Spring-Boot application to handle dynamically switching between multiple instances of the Mongo driver. The app already has Spring Boot MongoDB Repositories configured, but now we need to implement sass (dynamical ...

Setting up Jest configuration for integrating supertest with Vue framework

I have a unique application that utilizes both vue and express. I have separate tests for each, allowing me to run either a vue test or an express test independently. Below is the customized jest config file. This configuration works perfectly for Vue tes ...