capacitor-zeroconf-lt
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

capacitor-zeroconf-lt

Capacitor ZeroConf plugin (based on capacitor-zeroconf)

This plugin allows you to browse and publish ZeroConf/Bonjour/mDNS services from applications developed using Ionic's Capacitor.

This is not a background service. When the cordova view is destroyed/terminated, publish and watch operations are stopped.

Android, iOS and Electron platforms are supported.

The has been ported from Cordova ZeroConf Plugin.

Install

npm install capacitor-zeroconf-lt
npx cap sync

or

yarn add capacitor-zeroconf-lt
yarn cap sync

API

getHostname()

getHostname() => Promise<{ hostname: string; }>

Returns: Promise<{ hostname: string; }>


register(...)

register(request: ZeroConfRegisterRequest) => Promise<void>
Param Type
request ZeroConfRegisterRequest

unregister(...)

unregister(request: ZeroConfUnregisterRequest) => Promise<void>
Param Type
request ZeroConfUnregisterRequest

stop()

stop() => Promise<void>

watch(...)

watch(request: ZeroConfWatchRequest, callback?: ZeroConfWatchCallback | undefined) => Promise<CallbackID>
Param Type
request ZeroConfWatchRequest
callback ZeroConfWatchCallback

Returns: Promise<string>


unwatch(...)

unwatch(request: ZeroConfUnwatchRequest) => Promise<void>
Param Type
request ZeroConfWatchRequest

close()

close() => Promise<void>

Interfaces

ZeroConfRegisterRequest

Prop Type
port number
props { [key: string]: string; }

ZeroConfUnregisterRequest

Prop Type
name string

ZeroConfWatchRequest

Prop Type
type string
domain string

Example

Below you can find an example of what a React component could look like:

const MdnsService: React.FC<Props> = (props) => {
  const options = { type: '_castor-display._tcp.', domain: 'local.' };

  useEffect(() => {
    let listener: any;

    const onDiscover = (result: ZeroConfWatchResult) => {
      console.log('mDNS listener result:', result);
    };

    (ZeroConf as any)
      .addListener('discover', onDiscover)
      .then((res: any) => (listener = res));

    ZeroConf.watch(options)
      .then((res: any) => console.log('mDNS success:', res))
      .catch((err: any) => console.log('mDNS error:', err));

    return () => {
      if (listener) {
        console.log('removing listener', listener);
        if (listener.remove) {
          console.log('... using remove()');
          listener.remove();
        } else {
          (ZeroConf as any).removeListener(listener);
        }
      }
      ZeroConf.unwatch(options)
        .then(() => {
          console.log('unwatch success');
          // need to close for Android to rescan
          // TODO: try fixing Android implementation
          ZeroConf.close().then(() => console.log('close success'));
        })
        .catch((err: any) => console.log('unwatch error:', err));
    };
  }, []);

  return <></>;
};

Package Sidebar

Install

npm i capacitor-zeroconf-lt

Weekly Downloads

1

Version

1.0.6

License

MIT

Unpacked Size

77.5 kB

Total Files

32

Last publish

Collaborators

  • matallui