Sherzod
Sherzod

Reputation: 5131

How to send a request to another domain attaching audio file using node.js?

Here's the flow that I am trying to achieve:

1) User uploads an audio file to server1
2) Server1 receives that audio file and sends it to server2 in different domain
3) Server2 converts audio file to text
4) Server2 responds back to server1 with text
5) Server1 displays the text to the user

Speech to text conversion on server2 is done. I am stuck on sending the audio file and waiting for response. I know how to send request using GET, but I don't think I can use it with audio files.

How can I send an audio file to another server using node.js?

I am pretty new to node.js, so any help would be appreciated. Thanks.

UPDATE:
Server2 uses REST API and expects the file to be POST'ed.

Upvotes: 0

Views: 3030

Answers (2)

kgilpin
kgilpin

Reputation: 2226

This is a good use case for messaging. I'd suggest a flow like this:

  • User posts the File from Browser to Server A.
  • Server A stores the File.
  • Server A creates a Job to convert the File to text and sends it to a job Queue. The Job contains the location of the File.
  • Server A creates a Channel (e.g. PubNub or Pusher).
  • Server A returns the Channel id to the browser.
  • Server B is actually a pool (can be many workers) listening on the Queue.
  • A pool worker pulls the job off the Queue and processes it.
  • The pool worker saves the text file.
  • The pool worker sends a message to the Channel Id containing the location and/or body of the text file.
  • The Browser gets the message notification and shows it to the user

Some weaknesses of direct HTTP communication between Server A and Server B are:

  • Tight coupling between the APIs and endpoints of Server A and Server B.
  • How are Server A and Server B granted permission to talk to each other?
  • What happens if Server B needs to be taken down for a while? With a message queue, the jobs accumulate until Server B is back up, then resume. With HTTP communication, there are failures until Server B resumes.
  • What if you need to add more Server Bs? Now you are messing around with HTTP reverse proxies, load balancing, etc, and your problem of taking Server Bs down for maintenance is compounded.

Upvotes: 0

maerics
maerics

Reputation: 156464

It totally depends on how the remote server expects to receive the audio file. Assuming that it has some sort of RESTful web service interface whereby the file contents are POST'ed to some URL, you might be able to do something like this:

fs.readFile('/path/to/my/audiofile.wav', function (err, data) {
  if (err) throw err;
  var options = {
    host: 'remotehost.com',
    path: '/upload/wav',
    method: 'POST',
    headers: { 'Content-Type': 'audio/wav' }
  };
  var req = http.request(options, function(res) {
    // Handle a successful response here...
  });
  req.on('error', function(e) {
    // Handle an error response here...
  });
  // Write the audio data in the request body.
  req.write(data);
  req.end();
});

Again, it completely depends on how the server wants you to send the data. It may expect a completely different protocol (or method, or path), authentication, encoding, or any number of specifics which would completely change the viability of my sample answer.

Upvotes: 2

Related Questions