Module that enables the transfer of large files between two devices over Wi-Fi.
npm install @arekjaar/react-native-transfer-big-files
Import the required methods from the library:
import {
discoverPeers,
subscribeDiscoverPeer,
stopDiscoverPeers,
receiveMessage,
sendMessageTo,
receiveFile,
sendFileTo,
subscribeOnFileCopyEnd,
subscribeOnProgressUpdates
} from 'react-native-transfer-big-files';
import * as React from 'react';
import { StyleSheet, View, Text, AppState, FlatList, Button } from 'react-native';
// Import methods from the library
import {
discoverPeers,
subscribeDiscoverPeer,
stopDiscoverPeers,
receiveMessage,
sendMessageTo,
receiveFile,
sendFileTo,
subscribeOnFileCopyEnd,
subscribeOnProgressUpdates
} from 'react-native-transfer-big-files';
export default function App() {
// State variables
const [peers, setPeers] = React.useState([]);
const [message, setMessage] = React.useState('');
const [progress, setProgress] = React.useState('');
const [received, setReceived] = React.useState('');
// Subscribe to discover peers event
React.useEffect(() => {
const discoverPeerSubscription = subscribeDiscoverPeer((peers) => {
setPeers(JSON.parse(peers));
});
discoverPeers('ip');
return () => {
discoverPeerSubscription.remove();
};
}, []);
// Handle message received
const catchMessage = async () => {
const message = await receiveMessage();
setMessage(message);
};
// Handle file received
const catchFile = () => {
receiveFile(`/data/data/com.transferbigfilesexample/cache/`, 'document.pdf', true)
.then(() => setReceived('File received successfully'))
.catch(() => setReceived('Error receiving file'));
};
// Render method
return (
<View style={styles.container}>
{/* UI components */}
</View>
);
}
// Styles
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 20,
backgroundColor: '#ffffff',
color: '#000000'
},
});
Discover peers on the network.
Subscribe to discover peer events.
Stop discovering peers.
Send a message to a specific device.
Receive a message from another device.
Send a file to a specific device.
Receive a file from another device.
Subscribe to file transfer progress updates.
Subscribe to file copy end events.
MIT MIT License
Copyright (c) 2024 Arekjaar
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Made with create-react-native-library