bun
bun copied to clipboard
Implement dgram
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.
This would also be nice to have for using @discordjs/voice to stream audio (especially since ytdl-core is working now)
@Jarred-Sumner How's this going?
Just tested some of my code again on bun 0.7.4 and this is needed.
Kudos.
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.
This would be awesome for discord/voice, waiting for this!
This would be awesome for discord/voice, waiting for this!
Yes. I'm have some difficult to use discord/voice or discord-player.
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
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.
node-ssdp relies on dgram
dd-trace
(Datadog APM) also relies on dgram
https://github.com/DataDog/dd-trace-js/blob/8cf126c82cdeccb54ae119249c7905ab06a05d94/packages/dd-trace/src/dogstatsd.js#L5
also bittorrent-dht relies on dgram. :( this should be prioritized.
discord voice protocol uses UDP, @discordjs/voice uses dgram, this should be done ASAP (after bug fixes)
I reviewed #159, but it seems like this is not on the roadmap, is it? @Jarred-Sumner
Came here looking for this since it's a blocking issue for doing all sorts of IoT stuff.
I would really need it for Webtorrent. 🙏
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.
Does anyone working on this? I could try implement something...
A quick note: UDP support also implies multicast ;)
this library that I want to use https://github.com/alxhotel/chromecast-api needs this
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 )
Up! Really need this for Bonjour (mDNS)
Looking forward to the datagram.
Up
It would be great for IoT ad-hoc servers listening for UDP messages. Thanks.
As a temporary solution, this can be implemented via FFI
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.
As a temporary solution, this can be implemented via FFI
What's FFI?
As a temporary solution, this can be implemented via FFI
What's FFI?
bridging from a .SO
/.DLL
.
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).