Using Udp within expo

Hey Guys is there a way for me to use UDP on expo without having to eject?

basically I want to be able to use this:
** GitHub - tradle/react-native-udp: node's dgram for react-native
or
** the dgram function that ships with nodejs

without having to link and stuff

You’ll have to detach to use that module right now.

Any plans of supporting udp in expo ?

It doesnt work even with ejected expo for me

installed on just created expo bare project and linked. works well with react-native run-ios but doesn’t work at all on expo start.

I’m also looking forward to this, as I’m trying to implement an NTP client. react-native-udp fails with [Unhandled promise rejection: TypeError: null is not an object (evaluating 'Sockets.createSocket')]

Yes same
How to fix this?

Hi @dghub

In the past you would have had to eject to use react-native-udp. Now you should be able to just install the npm and then build with EAS Build.

It will not work in Expo Go.

EDIT: I’ve just given it a try on Android (based on the example here). Disclaimer: I changed the code to use hooks and I may have introduced some bugs, but it seems to work.

import "expo-dev-client";
import { useEffect, useState } from "react";
import { StatusBar } from "expo-status-bar";
import { ScrollView, StyleSheet, Text, View } from "react-native";
import dgram from "react-native-udp";

function randomPort() {
  return (Math.random() * 60536) | (0 + 5000); // 60536-65536
}

// only works for 8-bit chars
function toByteArray(obj) {
  var uint = new Uint8Array(obj.length);
  for (var i = 0, l = obj.length; i < l; i++) {
    uint[i] = obj.charCodeAt(i);
  }

  return new Uint8Array(uint);
}

export default function App() {
  const [chatter, setChatter] = useState([]);

  const updateChatter = (msg) => {
    setChatter((prev) => prev.concat([msg]));
  };

  useEffect(() => {
    let a = dgram.createSocket("udp4");
    let aPort = randomPort();
    a.bind(aPort, function (err) {
      if (err) throw err;
      updateChatter("a bound to " + JSON.stringify(a.address()));
    });

    let b = dgram.createSocket("udp4");
    var bPort = randomPort();
    b.bind(bPort, function (err) {
      if (err) throw err;
      updateChatter("b bound to " + JSON.stringify(b.address()));
    });

    a.on("message", function (data, rinfo) {
      var str = String.fromCharCode.apply(null, new Uint8Array(data));
      updateChatter("a received echo " + str + " " + JSON.stringify(rinfo));
      a.close();
      a = null;
      b.close();
      b = null;
    });

    b.on("message", function (data, rinfo) {
      var str = String.fromCharCode.apply(null, new Uint8Array(data));
      updateChatter("b received " + str + " " + JSON.stringify(rinfo));

      // echo back
      b.send(data, 0, data.length, aPort, "127.0.0.1", function (err) {
        if (err) throw err;
        updateChatter("b echoed data");
      });
    });

    b.once("listening", function () {
      var msg = toByteArray("hello");
      a.send(msg, 0, msg.length, bPort, "127.0.0.1", function (err) {
        if (err) throw err;
        updateChatter("a sent data");
      });
    });

    return () => {
      if (a) a.close();
      if (b) b.close();
    };
  }, []);

  return (
    <View style={styles.container}>
      <ScrollView>
        {chatter.map((msg, index) => (
          <Text key={index} style={styles.welcome}>
            {msg}
          </Text>
        ))}
      </ScrollView>
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#f5fcff",
    paddingTop: 20,
  },
  welcome: {
    fontSize: 20,
    margin: 10,
    borderWidth: 1,
    borderRadius: 5,
    padding: 5,
  },
});
1 Like

Thanks for your post. I teach a night class in App Development and we use Expo. I wanted the class to connect to a Tello Drone over a Udp web socket and got a bit stuck. Your post sent me in the right direction. The class finishes in a few weeks but I’m confident the drone will fly. I have sockets working now and the students have built the ui. The two should come together this week.

1 Like