Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

dgram-as-promised

dex4er436MIT6.0.0TypeScript support: included

Promisify dgram module

promise, dgram

readme

dgram-as-promised

GitHub CI Trunk Check Coverage Status npm

This module provides promisified version of the standard dgram class. The API is the same as for standard dgram, except bind, close and send methods which return Promise object.

Requirements

This module requires ES2021 with Node >= 16.

Installation

npm install dgram-as-promised

Additionally for Typescript:

npm install -D @types/node

Usage

dgram-as-promised can be used similarly to the standard dgram module.

Example:

import DgramAsPromised from "dgram-as-promised"

const socket = DgramAsPromised.createSocket("udp4")

const MEMBERSHIP = "224.0.0.1"
const PORT = 41234

const message = Buffer.from("ABCDEFGH")

bind

Method bind returns Promise object which resolves to address info when listening event is emitted.

const address = await socket.bind()
console.log(`Socket is listening on ${address.address}:${address.port}`)

socket.setBroadcast(true)
socket.setMulticastTTL(128)

socket.addMembership(MEMBERSHIP)
console.log("Membership is set")

send

Method send returns Promise object which is fulfilled when the message has been sent.

const bytes = await socket.send(message, 0, message.length, PORT, MEMBERSHIP)
console.log(`Message is sent (${bytes} bytes)`)

recv

Method recv returns Promise object which resolves to the object with msg and rinfo properties as from message event or resolves to undefined when the socket is already closed.

It throws an error if a timeout occurs and it was set by setTimeout method.

const packet = await socket.recv()
if (packet) {
  console.log(`Received message: ${packet.msg.toString()}`)
  console.log(`Received ${packet.rinfo.size} bytes`)
}

close

Method close returns Promise object which resolves when close event is emitted or the socket is already closed.

await socket.close()
console.log("Socket is closed")

setTimeout

socket = socket.setTimeout(ms)

Set the timeout used by recv method for the idle socket and after this timeout, the recv method will reject a Promise with a timeout error.

After the error, the socket is not closed and calling another recv method will reset the timeout.

The method returns this object.

Example:

socket.setTimeout(1000)
await socket.recv()

iterate

Method iterate and the socket object returns an asynchronous iterator which will call recv method until the socket is closed.

for await (const packet of socket) {
  console.info(packet.msg.toString())
  // Close socket if Ctrl-D is in the message
  if (packet.msg.indexOf(4) !== -1) {
    await socket.close()
  }
}

destroy

Method destroy cleans internal listeners.

socket = socket.destroy()

The method returns this object.

License

Copyright (c) 2016-2024 Piotr Roszatycki mailto:piotr.roszatycki@gmail.com

MIT

changelog

Changelog

v6.0.0 2024-06-22

  • Converted to ESM module.
  • Requires Node >= 16.
  • addMembership, setBroadcast, setTTL, setMulticastTTL, setMulticastInterface, setMulticastLoopback, dropMembership, setRecvBufferSize, setSendBufferSize and destroy return this.
  • address method doesn't return string anymore; closes: dex4er/js-dgram-as-promised#51
  • setTimeout method sets timeout for recv method; closes: dex4er/js-dgram-as-promised#55

v5.0.1 2020-10-16

  • Library works without enabled "esModuleInterop" options. Fixes #38.

v5.0.0 2020-10-16

  • recv returns undefined when socket is closed.
  • New iterate method returns asynchrounous iterator.
  • New destroy method cleans internal listeners.

v4.0.0 2020-10-15

  • bind method rejects on error. Fixes #42.
  • recv method resolves on message event. Fixes #39.
  • Requires Node >= 10.
  • Uses eslint instead of tslint.

v3.0.3 2019-10-08

  • Use mocha-steps and cross-env for testing.

v3.0.2 2019-07-10

  • Updated depencencies.

v3.0.1 2019-06-04

  • Minor tweaks in README.
  • Added source map to the package.

v3.0.0 2019-05-08

  • Typings compatible with Node v12.
  • This module now exports default DgramAsPromised class whith createSocket as a static method.
  • address method returns AddressInfo | string.
  • send method accepts Uint8Array.

v2.0.0 2018-09-09

  • Rewritten in Typescript.
  • Requires Node >= 6.
  • Additional dgram option for createSocket.

v1.0.6 2018-07-15

  • jsdoc

v1.0.5 2018-07-15

  • npm audit clean

v1.0.4 2018-05-09

  • Adapt Typescript typings to Node 10.

v1.0.3 2018-04-03

  • Use mock-require and mocked dgram module for tests.

v1.0.2 2018-03-19

  • Tweak README file.

v1.0.1 2018-03-19

  • Bugfix for bind method.

v1.0.0 2017-10-23

  • Do not use any-promise.
  • bind method accepts address and port options and resolves to address info.
  • send method resolves to number of sent bytes.
  • The rest of dgram methods have been implemented.
  • For Node < 6 --harmony flag is required.
  • Typescript typings.

v0.1.2 2017-06-22

v0.1.1 2017-02-19

  • Upgraded tap-given

v0.1.0 2017-02-18

  • ES6
  • BDD tests
  • Relicensed to MIT

v0.0.2 2016-07-31

  • Corrected README and LICENSE

v0.0.1 2016-07-31

  • Initial release