bun icon indicating copy to clipboard operation
bun copied to clipboard

Implement dgram

Open Jarred-Sumner opened this issue 1 year ago • 87 comments

Jarred-Sumner avatar Dec 19 '22 17:12 Jarred-Sumner

Yes please! I'm working on an open source UPnP server package which uses dgram for some of its connection backend. It would be great if we could take advantage of bun's much improved speed in sqlite3 and general code execution, but we can't yet until dgram is implemented.

JL102 avatar May 17 '23 18:05 JL102

This would also be nice to have for using @discordjs/voice to stream audio (especially since ytdl-core is working now)

deadlinecode avatar Jul 06 '23 09:07 deadlinecode

@Jarred-Sumner How's this going?

ezShroom avatar Aug 14 '23 16:08 ezShroom

Just tested some of my code again on bun 0.7.4 and this is needed.

Kudos.

pcfreak30 avatar Aug 15 '23 22:08 pcfreak30

I've just tried on Bun 0.8 without any luck: NotImplementedError: node:dgram createSocket is not yet implemented in Bun.

This seems to be the blocker for one of our app.

daviddutch avatar Aug 24 '23 04:08 daviddutch

This would be awesome for discord/voice, waiting for this!

joetifa2003 avatar Aug 27 '23 18:08 joetifa2003

This would be awesome for discord/voice, waiting for this!

Yes. I'm have some difficult to use discord/voice or discord-player.

meluiz avatar Aug 29 '23 05:08 meluiz

I tried Bun 1.0 but it looks like our https://www.npmjs.com/package/hot-shots dependency doesn't work due to lack of node:dgram support

hpx7 avatar Sep 08 '23 14:09 hpx7

Well maybe we can start the discussion of whats needed to implement.

Cursory look at the node docs for dgram shows 1 type (Socket) with 19 methods, 5 events, and 2 constructors.

jedahan avatar Sep 08 '23 17:09 jedahan

node-ssdp relies on dgram

onionhammer avatar Sep 08 '23 19:09 onionhammer

dd-trace (Datadog APM) also relies on dgram

https://github.com/DataDog/dd-trace-js/blob/8cf126c82cdeccb54ae119249c7905ab06a05d94/packages/dd-trace/src/dogstatsd.js#L5

billdybas avatar Sep 09 '23 19:09 billdybas

also bittorrent-dht relies on dgram. :( this should be prioritized.

Zibri avatar Sep 09 '23 22:09 Zibri

discord voice protocol uses UDP, @discordjs/voice uses dgram, this should be done ASAP (after bug fixes)

coffeeispower avatar Sep 10 '23 01:09 coffeeispower

I reviewed #159, but it seems like this is not on the roadmap, is it? @Jarred-Sumner

Saissaken avatar Sep 10 '23 08:09 Saissaken

Came here looking for this since it's a blocking issue for doing all sorts of IoT stuff.

rcarmo avatar Sep 10 '23 14:09 rcarmo

I would really need it for Webtorrent. 🙏

MrDanielHarka avatar Sep 10 '23 15:09 MrDanielHarka

Bun can't be a drop-in replacement for really any of my projects without dgram support since we use dd-trace and hot-shots.

kalanchoej avatar Sep 11 '23 14:09 kalanchoej

Does anyone working on this? I could try implement something...

Reusek avatar Sep 11 '23 15:09 Reusek

A quick note: UDP support also implies multicast ;)

rcarmo avatar Sep 11 '23 16:09 rcarmo

this library that I want to use https://github.com/alxhotel/chromecast-api needs this

oliverlevay avatar Sep 11 '23 16:09 oliverlevay

A lot of us working with IoT and robotics really need this. I hope it gets prioritized, since is not on the roadmap (as in #159 )

Marcos-Rollon avatar Sep 12 '23 08:09 Marcos-Rollon

Up! Really need this for Bonjour (mDNS)

alessandroamella avatar Sep 12 '23 15:09 alessandroamella

Looking forward to the datagram.

LIMPIX31 avatar Sep 14 '23 06:09 LIMPIX31

Up

BlusteryS avatar Sep 14 '23 14:09 BlusteryS

It would be great for IoT ad-hoc servers listening for UDP messages. Thanks.

nahuelon avatar Sep 14 '23 15:09 nahuelon

As a temporary solution, this can be implemented via FFI

LIMPIX31 avatar Sep 14 '23 17:09 LIMPIX31

As a temporary solution, this can be implemented via FFI

At what cost for developers adopting bun? Not everyone wants to do their own socket code.

rcarmo avatar Sep 14 '23 17:09 rcarmo

As a temporary solution, this can be implemented via FFI

What's FFI?

JL102 avatar Sep 14 '23 18:09 JL102

As a temporary solution, this can be implemented via FFI

What's FFI?

bridging from a .SO/.DLL.

pcfreak30 avatar Sep 14 '23 18:09 pcfreak30

I don't know much about low level stuff, but this could be a starting point. This is a simple code to listen for data from a socket. I haven't implemented sending data yet due to some unrelated issues.

Lib (Rust)

use {
  std::{
    net::UdpSocket,
    sync::{Arc, Mutex}, 
  },
  lazy_static::lazy_static,
  safer_ffi::prelude::*,
};

lazy_static! {
  static ref SOCKETS: Arc<Mutex<Vec<UdpSocket>>> = Arc::new(Mutex::new(Vec::new()));
}

fn store_socket(socket: UdpSocket) -> usize {
  let mut sockets_store = SOCKETS.lock().unwrap();
  sockets_store.push(socket);

  sockets_store.len() - 1
}

#[ffi_export]
fn bind<'a>(addr: char_p::Ref<'a>) -> usize {
  let socket = UdpSocket::bind(addr.to_str()).unwrap();

  store_socket(socket)
}

#[ffi_export]
fn recv_from<'a>(socket_idx: usize, buf: c_slice::Mut<'a, u8>) -> usize {
  let sockets_store = SOCKETS.lock().unwrap();
  let socket = sockets_store.get(socket_idx).unwrap();

  socket.recv_from(buf.as_slice()).unwrap().0
}

Bun

const { symbols, close } = dlopen(`${import.meta.dir}/libbungram.${suffix}`, {
  bind: {
    args: [FFIType.cstring],
    returns: FFIType.u32,
  },
  recv_from: {
    args: [FFIType.u32, FFIType.ptr],
    returns: FFIType.u32,
  },
})

const addr = Buffer.from('0.0.0.0:10011', 'utf8')

const socketIdx = symbols.bind(ptr(addr))

const buf = ptr(Buffer.alloc(2048))

while (true) {
  const len = symbols.recv_from(socketIdx, buf)

  const payload = toArrayBuffer(buf, 0, len)
  const stringified = Buffer.from(payload).toString('utf8')

  console.log(`Payload(${len}): ${stringified}`)

  if (stringified.startsWith('exit')) {
    break
  }
}

close()

Besides, Bun does not have the ability to call FFI without blocking the main thread (at least it is not written in the documentation).

LIMPIX31 avatar Sep 15 '23 17:09 LIMPIX31