How to use the azure-iot-device.ModuleClient.fromConnectionString function in azure-iot-device

To help you get started, we’ve selected a few azure-iot-device examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / azure-iot-sdk-node / device / edge-sample / edge_sample_module.js View on Github external
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').ModuleClient;
var Message = require('azure-iot-device').Message;
var fs = require('fs');

var connectionString = process.env.EdgeHubConnectionString;

var client = Client.fromConnectionString(connectionString, Protocol);
console.log('got client');

client.on('error', function (err) {
  console.error(err.message);
});

client.setOptions({
  ca: fs.readFileSync(process.env.EdgeModuleCACertificateFile).toString('ascii')
}, function(err) {
  if (err) {
    console.log('error:' + err);
  } else {
    // connect to the edge instance
    client.open(function (err) {
      if (err) {
        console.error('Could not connect: ' + err.message);
github Azure / azure-iot-sdk-node / device / samples / module_send_event.js View on Github external
const run = async() => {
    try {
        console.log('Initializing Module Client.');
        const moduleClient = ModuleClient.fromConnectionString(process.env.DEVICE_CONNECTION_STRING, Mqtt);
        // We use the iot hub connection string to create the client rather than the actual event hub end point
        console.log('Initializing Event Hub Client.');
        const eventHubClient = await EventHubClient.createFromIotHubConnectionString(process.env.IOTHUB_CONNECTION_STRING); 
        console.log('Initializing HTTP Device Client.');
        const httpClient = HttpClientFromConnectionString(process.env.DEVICE_CONNECTION_STRING);
        console.log('Initialized clients!');

        console.log('Getting information from Event Hub.');
        const partitionIds = await eventHubClient.getPartitionIds(); //read more about partitions https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-features#partitions
        const startingPosition = EventPosition.fromEnqueuedTime(Date.now() - 1000); //subtracting a second to account for delay
        
        const eventLabel = 'beepBoop'; //label our events
        const sendDummyMessage = m => moduleClient.sendOutputEvent(eventLabel, m); //send helper

        console.log('Creating messages to send to via Module Client');
        //generate some messages
github Azure / azure-iot-sdk-node / edge-e2e / wrapper / nodejs-server-server / glue / moduleGlue.js View on Github external
return glueUtils.makePromise('moduleConnectPut', function(callback) {
    var client = ModuleClient.fromConnectionString(connectionString, glueUtils.transportFromType(transportType));
    var connectionId = objectCache.addObject('moduleClient', client);
    glueUtils.setOptionalCert(client, caCertificate, function(err) {
      glueUtils.debugFunctionResult('glueUtils.setOptionalCert', err);
      if (err) {
        callback(err);
      } else {
        debug('calling moduleClient.open');
        client.open(function(err) {
          glueUtils.debugFunctionResult('client.open', err);
          if (err) {
            objectCache.removeObject(connectionId);
            callback(err);
          } else {
            callback(null, {connectionId: connectionId});
          }
        });