title: How to save real time video stream and convert it to MP4 using Websocket and Javascript MediaRecorder API?
body: <p>I am trying to upload real time video stream using Javascript and .NET Web Socket. I am passing Base64 encoded data each second to web socket server where its appending data into a single file which is in webm format. At times video is not getting uploaded cause of unknown error or it gets partially uploaded. Can someone help with best approach to upload video on server real time and convert it to MP4 without paid third party tools. Am appending code I am using below-</p>
  
  <pre><code>//js code
  
  var handleDataAvailable = function(event) {
        if (event.data &amp;&amp; event.data.size &gt; 0) {
            try{          
                  var reader = new window.FileReader();                  
                  reader.onloadend = function () {
                    var base64data = reader.result;
                    if (ws.readyState == WebSocket.OPEN){
                      ws.send(base64data);
                    }
                  }
                  reader.readAsDataURL(event.data);            
            }catch(ex){
  
            }      
        }
      }
  const SERVER_URL = "ws://localhost:5029";
  var ws = new WebSocket(SERVER_URL);
  var mediaRecorder = new MediaRecorder(outputStream, options);
  mediaRecorder.ondataavailable = handleDataAvailable;
  
  // C# code
  // System.Net.WebSockets;
  // websocket connection handling code
  //
  
  //on message event of websocket
  public async Task OnMessage(string message)
  {
      var base64Data = Regex.Match(message, @"data:video/(?&lt;type&gt;.+?),(?&lt;data&gt;.+)").Groups["data"].Value;
      var ByteArray = Convert.FromBase64String(base64Data);
      string path = "~//TempUploads//Recordings//test.webm";
      if (!Directory.Exists(HttpContext.Current.Server.MapPath(path)))
      {
          Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
      }
  
      using (var stream = new FileStream(HttpContext.Current.Server.MapPath(path), FileMode.Append))
      {
        try
        {
          stream.Write(ByteArray, 0, ByteArray.Length);
        }
        catch (Exception ex)
        {
  
  
        }
  
      }
  
  }
  </code></pre>