Node.js soap.createClientAsync() and wsdl as a string

2 min read 05-10-2024
Node.js soap.createClientAsync() and wsdl as a string


Consuming SOAP Web Services with Node.js: Using soap.createClientAsync() and WSDL as a String

Problem: You need to interact with a SOAP web service in your Node.js application, but the WSDL (Web Services Description Language) is provided as a string, not a file path.

Solution: Node.js's soap package offers the createClientAsync() function, which allows you to create a client object directly from a WSDL string, eliminating the need for a file. This approach proves incredibly useful when dealing with dynamically generated WSDLs or when you want to avoid writing the WSDL to a file.

Scenario:

Let's say you're building a Node.js application that needs to communicate with a SOAP web service. You have the WSDL as a string, stored in a variable wsdlString.

const soap = require('soap');
const wsdlString = `
  <wsdl:definitions ... >
    ...
  </wsdl:definitions>
`;

// Traditional approach using a file path
// soap.createClient(wsdlFilePath, (err, client) => {
//   // ...
// });

Using soap.createClientAsync() with WSDL String:

To use soap.createClientAsync() with your WSDL string, follow these steps:

  1. Import the soap package:

    const soap = require('soap');
    
  2. Call soap.createClientAsync() with the WSDL string:

    soap.createClientAsync(wsdlString, (err, client) => {
      if (err) {
        console.error('Error creating SOAP client:', err);
        return;
      }
    
      // Access the client object to make calls
      client.yourServiceMethod(parameters, (err, response) => {
        if (err) {
          console.error('Error calling service method:', err);
        } else {
          console.log('Service response:', response);
        }
      });
    });
    
  3. Use the client object to call the service methods:

    • Once the client is created, you can call the various methods exposed by the web service using the client object.
    • The parameters for the service method should be passed as an object.

Advantages of using soap.createClientAsync():

  • Flexibility: No need to create and manage WSDL files.
  • Dynamic WSDLs: Can easily handle WSDLs dynamically generated at runtime.
  • Code cleanliness: Keeps the code compact and focused on the web service interaction.

Example:

Imagine you have a SOAP web service for calculating currency conversions. The WSDL string is stored in wsdlString.

soap.createClientAsync(wsdlString, (err, client) => {
  if (err) {
    console.error('Error creating SOAP client:', err);
    return;
  }

  client.CurrencyConverter.convert(
    {
      fromCurrency: 'USD',
      toCurrency: 'EUR',
      amount: 100
    },
    (err, response) => {
      if (err) {
        console.error('Error calling convert method:', err);
      } else {
        console.log('Conversion result:', response.result);
      }
    }
  );
});

Conclusion:

Using soap.createClientAsync() with a WSDL string simplifies SOAP web service consumption in Node.js. It provides a cleaner and more flexible approach, especially when dealing with dynamic WSDLs.

Remember:

  • Ensure your WSDL string is well-formed and valid.
  • Refer to the documentation of the soap package for details on the available methods and options.

References: