react-native-udp icon indicating copy to clipboard operation
react-native-udp copied to clipboard

Not receiving unicast message in release. Works in debug.

Open yullm opened this issue 1 year ago • 2 comments

Description

I have an app that sends out a broadcast message to companion server apps on the same network. The servers should be able to see this message then send a message back to the app directly with unicast. The whole system works when in debug mode but once the I build the release APK the app no longer receives the messages from the servers.

Edit: Experiencing the issue on both iOS and Android. Debug works as expected, Release does not.

Things we know:

  • The app still sends the broadcast message in both versions
  • The message from the server is being sent.
  • Socket is being bound in release
  • Testing on device

Release Permissions same as Debug

Image

Expected behaviour (Debug):

Image

Shows binding and most recent message

Release behaviour:

Image

No message

Wireshark shows message is sent

Image

Code

import dgram from 'react-native-udp';
import { NetworkInfo } from 'react-native-network-info';
import { useEffect, useRef, useState } from 'react';
import { useID } from '../Context/IDContext';
import { ServerData, useServerData } from '../Context/ServerDataContext';
import { useSocket } from '../Context/SocketContext';
import { AppState, Platform, Text } from 'react-native';
import UdpSocket from 'react-native-udp/lib/types/UdpSocket';

const bindingPort = 6537;
const broadcastPort = 6536;
var broadcastAddress = "";
const broadcastFrequency = 10000;

var udpSocket:UdpSocket;

var detailText = "";

const getBroadcastAddress = async(): Promise<String> => {
    //removed for space
};

const bindUDPSocket = ()=>{

    udpSocket = dgram.createSocket({type:"udp4",reusePort:true,debug:true})

    getBroadcastAddress().then(bAdd =>{broadcastAddress = bAdd.toString()});

    udpSocket.once('listening',()=>{
        if(Platform.OS != "android"){
            udpSocket.setBroadcast(true);
        }
        detailText = `UDP BOUND to ${udpSocket.address().address}:${udpSocket.address().port}`;
        console.log(`UDP BOUND to ${udpSocket.address().address}:${udpSocket.address().port}`);
    });

    udpSocket.on('error',(err:any)=>{
        console.log(err);
        udpSocket.close();
    });

    udpSocket.bind(bindingPort);
};

bindUDPSocket();

export const Discovery = () => {

    const idContext = useID();
    const serverDataContext = useServerData();
    const socketContext = useSocket();
    const changeTimer = useRef<NodeJS.Timeout>(undefined); 
    const broadcastTimer = useRef<NodeJS.Timeout>(undefined); 

    const [lastUPDMessage, setLastUDPMessage] = useState("");

    useEffect(()=>{
        clearInterval(broadcastTimer.current);
        clearTimeout(changeTimer.current);
        changeTimer.current = setTimeout(buildMessage,1000);
        broadcastTimer.current = setInterval(buildMessage,broadcastFrequency);

        return () => {
            clearInterval(broadcastTimer.current);
        }
    },[idContext?.uuid,idContext?.provenceID]);

    const handleUDPMessages = (message:any,remoteInfo:any)=>{
        try{
            const messageObj = JSON.parse(message.toString());
            console.log(messageObj); //<-- Logging message
            if('eventType' in messageObj){
                switch(messageObj.eventType){
                    case 'socket-connection-request':
                        const serverData = {address:remoteInfo.address,port:messageObj.port} as ServerData;
                        console.log(serverData);
                        serverDataContext?.dispatch({type:'set',serverData:serverData});
                        socketContext?.dispatch({type:'connect',serverData:serverData});
                        break;
                }
            }
            setLastUDPMessage(message.toString()); //<-- Drawing to screen
        }catch(err){
            console.log(err);
        }
    };

    useEffect(()=>{
        udpSocket.on('message',handleUDPMessages); //<--- getting message
        AppState.addEventListener('change',(state)=>{
            if(state == "active"){
                udpSocket.close();
                bindUDPSocket();
            }
        });
        return()=>{
            udpSocket.off('message',handleUDPMessages);
        };
    },[]);

    function buildMessage (){
        console.log(idContext?.provenceID,broadcastAddress);
        const message = JSON.stringify({uuid: idContext?.uuid, provenceID: idContext?.provenceID});
        udpSocket.send(message,undefined,undefined,broadcastPort,broadcastAddress);
    };

    return(<>
        <Text style={{color:"white"}}>{`${detailText},${lastUPDMessage}`}</Text>
    </>);
};

Relevant information

OS Android / iOS
react-native 0.79.2
react-native-udp 4.1.7

yullm avatar Jul 30 '25 11:07 yullm

@yullm assuming it doesn't present a security issue one option is android:usesCleartextTraffic="true" in the android manifest <application

redaxle avatar Oct 13 '25 01:10 redaxle

Yes this same goes true if you use http without the (s) at the end. As long as you don’t use it outside of your Local Area Network LAN that is perfectly legal because the attacker has to be physically in the area where your LAN works. For example has to be inside your home connected to your Wifi to be able to snoop your data.  In any case, all of the data servers and cloud like AWS requires you todo HTTPS if you ever communicate outside if your LAN. 

Sent from Yahoo Mail for iPhone

On Sunday, October 12, 2025, 9:48 PM, Red Axle @.***> wrote:

redaxle left a comment (tradle/react-native-udp#258) @yullm assuming it doesn't present a security issue one option is android:usesCleartextTraffic="true" in the android manifest <application

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

gibo77 avatar Oct 13 '25 19:10 gibo77