Skip to content

Use libp2p-mdns with Electron #880

Closed
@raphael10-collab

Description

@raphael10-collab

In an Electron-Typescript-React app when I add this line:

streamMuxer: [Mplex],

I get this error:

TS2322: Type '{ transport: any[]; connEncryption: Noise[]; streamMuxer: any[]; peerDiscovery: any[]; }' is not assignable to type  
'Libp2pModules'.
  Object literal may only specify known properties, and 'peerDiscovery' does not exist in type 'Libp2pModules'.
    30 |       connEncryption: [NOISE],
    31 |       streamMuxer: [Mplex],
  > 32 |       peerDiscovery: [MulticastDNS]
       |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    33 |     },
    34 |     config: {
    35 |       peerDiscovery: {

image

This is the code in src/app/components/App.tsx :

import Libp2p from 'libp2p';
import Websockets from 'libp2p-websockets';
import WebRTCStar from 'libp2p-webrtc-star';
import { NOISE } from 'libp2p-noise';
import Mplex from 'libp2p-mplex';
import Bootstrap from 'libp2p-bootstrap';
import KadDHT from 'libp2p-kad-dht';
import MulticastDNS from 'libp2p-mdns';

document.addEventListener('DOMContentLoaded', async () => {
  // Create our libp2p node
  const libp2p = await Libp2p.create({
    addresses: {
      // Add the signaling server address, along with our PeerId to our multiaddrs list
      // libp2p will automatically attempt to dial to the signaling server so that it can
      // receive inbound connections from other peers
      listen: [
        '/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star',
        '/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star'
      ]
    },
    modules: {
      transport: [Websockets, WebRTCStar],
      connEncryption: [NOISE],
      streamMuxer: [Mplex],
      peerDiscovery: [MulticastDNS]
    },
    config: {
      peerDiscovery: {
        // The `tag` property will be searched when creating the instance of your Peer Discovery service.
       // The associated object, will be passed to the service when it is instantiated.
        [Bootstrap.tag]: {
          enabled: true,
          list: [
            '/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
            '/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
            '/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
            '/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
            '/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
          ]
        },
       [MulticastDNS.tag]: {
         interval: 20e3,
         enabled: true
      }
    }
  })

/tools/webpack/webpack.renderer.js :

const rules = require('./webpack.rules');
const plugins = require('./webpack.plugins');
const aliases = require('./webpack.aliases');

module.exports = {
  // https://github.com/electron/electron/issues/9920
  //target: 'electron-renderer',
  target: 'web',
  module: {
    rules,
  },
  plugins: plugins,
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css'],
    alias: {
      // React Hot Loader Patch
      'react-dom': '@hot-loader/react-dom',
      // Custom Aliases
      ...aliases,
    },
  },
};

/tools/webpack/webpack.rules.js :

const inDev = process.env.NODE_ENV === 'development';

module.exports = [
  {
    // Add support for native node modules
    test: /\.node$/,
    use: 'node-loader',
  },
  {
    // Webpack asset relocator loader
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@marshallofsound/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },
  {
    // Typescript loader
    test: /\.tsx?$/,
    exclude: /(node_modules|\.webpack)/,
    use: {
      loader: 'ts-loader',
      options: {
        transpileOnly: true,
      },
    },
  },
  {
    // CSS Loader
    test: /\.css$/,
    use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
  },
  {
    // Less loader
    test: /\.less$/,
    use: [
      { loader: 'style-loader' },
      { loader: 'css-loader' },
      { loader: 'less-loader' },
    ],
  },
  {
    // Images Loader
    test: /\.(gif|jpe?g|tiff|png|webp|bmp)$/,
    use: [
      {
        loader: 'file-loader',
        options: {
          publicPath: 'images',
          outputPath: inDev ? 'images' : './main_window/images',
        },
      },
    ],
  },
];

/tools/webpack/webpack.main.js :

module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: ['./src/main.ts'],
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules'),
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
    alias: require('./webpack.aliases'),
  },
  // https://github.com/electron/electron/issues/9920
  target: 'electron-renderer'
};

/tools/webpack/webpack.plugins.js :

// eslint-disable-next-line @typescript-eslint/no-var-requires
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

// https://stackoverflow.com/questions/44008674/how-to-import-the-electron-ipcrenderer-in-a-react-webpack-2-setup
const webpack = require('webpack');

module.exports = [
  new ForkTsCheckerWebpackPlugin(),
  new webpack.ExternalsPlugin('commonjs', [
    'electron'
  ])
];

devDependencies :

"@electron-forge/cli": "6.0.0-beta.53",
"@electron-forge/maker-deb": "6.0.0-beta.53",
"@electron-forge/maker-rpm": "6.0.0-beta.53",
"@electron-forge/maker-squirrel": "6.0.0-beta.53",
"@electron-forge/maker-zip": "6.0.0-beta.53",
"@electron-forge/plugin-webpack": "6.0.0-beta.53",
"electron": "^11.2.1",
"typescript": "^4.0.2",
"webpack": "4"

dependencies:

"libp2p": "^0.30.6",
"libp2p-bootstrap": "^0.12.1",
"libp2p-kad-dht": "^0.20.6",
"libp2p-mdns": "^0.15.0",
"libp2p-mplex": "^0.10.2",
"libp2p-noise": "^2.0.4",
"libp2p-tcp": "^0.15.2",
"libp2p-webrtc-star": "^0.21.0",
"libp2p-websockets": "^0.15.0",
  • node version: v14.5.0
  • O.S. : Ubuntu 18.04.4 Desktop

If you need to reproduce the error,
I've put the simple repo here: https://github.com/raphael10-collab/Libp2pPlaying

Just git clone it ----> yarn ---> yarn start

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions