Skip to content
On this page

useNetworkInformation

The NetworkInformation.

State

The useNetworkInformation function exposes the following reactive state:

typescript
import { useNetworkInformation } from '@elonehoo/pistachio'

const {
  downlink,
  downlinkMax,
  effectiveType,
  rtt,
  saveData,
  supported,
  type
} = useNetworkInformation()
import { useNetworkInformation } from '@elonehoo/pistachio'

const {
  downlink,
  downlinkMax,
  effectiveType,
  rtt,
  saveData,
  supported,
  type
} = useNetworkInformation()
StateTypeDescription
downlinknumberReturns the effective bandwidth estimate in megabits per second, rounded to the nearest multiple of 25 kilobits per seconds.
downlinkMaxnumberReturns the maximum downlink speed, in megabits per second (Mbps), for the underlying connection technology.
effectiveTypestringReturns the effective type of the connection meaning one of slow-2g, 2g, 3g, or 4g. This value is determined using a combination of recently observed round-trip time and downlink values.
rttnumberReturns the estimated effective round-trip time of the current connection, rounded to the nearest multiple of 25 milliseconds.
saveDatabooleanReturns true if the user has set a reduced data usage option on the user agent.
supportedbooleanIf the current browser supports Network Information API
typestringReturns the type of connection a device is using to communicate with the network. It will be one of the following values: bluetooth, cellular, ethernet, none, wifi, wimax, other, unknown

Methods

The useNetworkInformation function exposes the following methods:

typescript
import { useNetworkInformation } from '@elonehoo/pistachio'

const { remove } = useNetworkInformation()
import { useNetworkInformation } from '@elonehoo/pistachio'

const { remove } = useNetworkInformation()
SignatureDescription
removeManually removes the event listener

Example

Network information

supported: false

downlink: 0

downlinkMax: 0

effectiveType: "none"

round-trip time: 0

saveData: false

type: "none"

vue
<script setup lang="ts">
import { useNetworkInformation } from '@elonehoo/pistachio'

const networkInformation = useNetworkInformation()
</script>

<template>
  <div>
    <h3>Network information</h3>

    <p>
      supported: <b>{{ networkInformation.supported }}</b>
    </p>
    <p>
      downlink: <b>{{ networkInformation.downlink }}</b>
    </p>
    <p>
      downlinkMax: <b>{{ networkInformation.downlinkMax }}</b>
    </p>
    <p>
      effectiveType: <b>{{ networkInformation.effectiveType }}</b>
    </p>
    <p>
      round-trip time: <b>{{ networkInformation.rtt }}</b>
    </p>
    <p>
      saveData: <b>{{ networkInformation.saveData }}</b>
    </p>
    <p>
      type: <b>{{ networkInformation.type }}</b>
    </p>
  </div>
</template>
<script setup lang="ts">
import { useNetworkInformation } from '@elonehoo/pistachio'

const networkInformation = useNetworkInformation()
</script>

<template>
  <div>
    <h3>Network information</h3>

    <p>
      supported: <b>{{ networkInformation.supported }}</b>
    </p>
    <p>
      downlink: <b>{{ networkInformation.downlink }}</b>
    </p>
    <p>
      downlinkMax: <b>{{ networkInformation.downlinkMax }}</b>
    </p>
    <p>
      effectiveType: <b>{{ networkInformation.effectiveType }}</b>
    </p>
    <p>
      round-trip time: <b>{{ networkInformation.rtt }}</b>
    </p>
    <p>
      saveData: <b>{{ networkInformation.saveData }}</b>
    </p>
    <p>
      type: <b>{{ networkInformation.type }}</b>
    </p>
  </div>
</template>

Released under the MIT License.