Need some help running through my codes to make WebRTC work. I am just trying to make a call from one device to the other. Realise I'm not throughly understanding WebRTC. My app crashes when the answer is emitted back.
The socket/emitter I am working with is actually
FeathersJS services. It is serving as my backend, whenever I need to emit, I send a create service and in my backend, feathersJS emit a customEmitter ['offer' 'answer' 'ice-candidate']. Although I am able to receive all emitted events, I am unable to get WebRTC to work. The below are my React-native code:
import React, { useContext, useEffect, useState } from 'react';
import {
View, SafeAreaView, Button, StyleSheet,
} from 'react-native';
import {
RTCPeerConnection,
RTCSessionDescription,
RTCView,
mediaDevices,
} from 'react-native-webrtc';
import ContextApp from '../../context/contextApp';
import FeathersApp, { Services } from '../../Feathers';
const styles = StyleSheet.create({
container: {
backgroundColor: '#313131',
justifyContent: 'space-between',
alignItems: 'center',
height: '100%',
},
text: {
fontSize: 30,
},
rtcview: {
justifyContent: 'center',
alignItems: 'center',
height: '40%',
width: '80%',
backgroundColor: 'black',
},
rtc: {
width: '80%',
height: '100%',
},
toggleButtons: {
width: '100%',
flexDirection: 'row',
justifyContent: 'space-around',
},
});
const VideoCallPage = ( props: any ) => {
const { userId } = props;
const { authState } = useContext(ContextApp);
const callService = FeathersApp.service(Services.CALL);
const [callAlert, setCallAlert] = useState<boolean>(false);
const [offerData, setOfferData] = useState<any>();
const configuration = {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]};
const localPC = new RTCPeerConnection(configuration);
const [localStream, setLocalStream] = useState<any>();
const [remoteStream, setRemoteStream] = useState<any>();
const sendToPeer = (messageType: any, payload: any) => {
callService.create({
callType: messageType,
sdp: payload,
caller: authState._id,
answerer: userId,
candidate: payload,
})
}
const createOffer = async () => {
const offer = await localPC.createOffer();
await localPC.setLocalDescription(offer).then(() => console.log('local offer succeeded'));
sendToPeer('offer', localPC.localDescription);
}
const createIceCandidate = () => {
localPC.onicecandidate = e => {
try {
console.log('localPC icecandidate:', e.candidate);
if (e.candidate) {
sendToPeer('ice-candidate', e.candidate)
}
} catch (err) {
console.error(`Error adding remotePC iceCandidate: ${err}`);
}
};
}
const startCall = async () => {
console.log('start call funtion')
const newStream = await mediaDevices.getUserMedia({
audio: true,
video: false,
});
setLocalStream(newStream);
createIceCandidate()
createOffer();
};
const recieveCall = async (incoming: any) => {
console.log('accepting call');
await localPC.setRemoteDescription(new RTCSessionDescription(incoming.sdp));
const answer = await localPC.createAnswer();
await localPC.setLocalDescription(answer).then(() => console.log('localPC desc succeeded'));
sendToPeer('answer', localPC.localDescription);
createIceCandidate();
localPC.addStream(localStream);
localPC.onaddstream = e => {
console.log('remotePC tracking with ', e);
if (e.stream && remoteStream !== e.stream) {
console.log('RemotePC received the stream', e.stream);
setRemoteStream(e.stream);
}
};
};
const recieveAnswer = (payload: any) => {
console.log('recieved answer: ', payload.candidate.sdp)
localPC.setRemoteDescription(new RTCSessionDescription(payload.candidate.sdp));
localPC.onaddstream = e => {
if (e.stream && remoteStream !== e.stream) {
console.log('RemotePC received the stream', e.stream);
setRemoteStream(e.stream);
}
};
}
// alert for any calls
useEffect(() => {
callService.on('ice-candidate', (incoming: any) => {
localPC.addIceCandidate(incoming.data.sdp);
localPC.addStream(localStream);
});
callService.on('offer', async (payload: any) => {
if (payload.data.answerer === authState._id) {
const newStreamForReciever = await mediaDevices.getUserMedia({
audio: true,
video: false,
});
setLocalStream(newStreamForReciever);
setOfferData(await payload.data);
setCallAlert(true);
}
});
callService.on('answer', async (payload: any) => {
if (payload.data.answerer === authState._id) {
console.log(userId, 'answered')
recieveAnswer(await payload.data);
}
});
// return (() => {
// callService.removeAllListener('ice-candidate');
// callService.removeAllListener('offer');
// callService.removeAllListener('answer');
// })
}, [callService]);
return (
<SafeAreaView style={styles.container}>
{!localStream && <Button title="Click to start CALL stream" onPress={startCall} />}
{callAlert && <Button title="Answer Call" onPress={() => recieveCall(offerData)} />}
{localStream && (
<View style={styles.toggleButtons}>
</View>
)}
<View style={styles.rtcview}>
{localStream && <RTCView style={styles.rtc} streamURL={localStream.toURL()} />}
</View>
<View style={styles.rtcview}>
{remoteStream && <RTCView style={styles.rtc} streamURL={remoteStream.toURL()} />}
</View>
</SafeAreaView>
)
};
export default VideoCallPage;
What is it that I am missing out??
Any guidance you can provide is greatly appreciated!
Related
I have a scrolling view of posts. Each post has a corresponding user and I have a header that shows the user info of the current visible post. With Flutter this was simple, I just wrapped the post widget with a visibility detector. With React Native this is not very easy. I've tried onViewableItemsChanged but, since I am using a fuction not a class, that causes an error. I also tried some solutions that used onScroll and onMomentumScrollEnd but those all just stayed at index 0. How can I get the current index that is fully visible? If needed, I am fine with splitting up the pagination functions so I can just have a class with the UI and use onViewableItemsChanged but I don't know how to do that because the handleLoadMore function is used in the UI.
export default function PostsListView() {
const [users, setUsers] = useState<User[]>([]);
const [posts, setPosts] = useState<Post[]>([]);
const [page, setPage] = useState(1);
const [loading, setLoading] = useState(true);
const [hasMore, setHasMore] = useState(true);
const onScroll = useCallback((event: any) => {
const slideSize = event.nativeEvent.layoutMeasurement.width;
const index = event.nativeEvent.contentOffset.x / slideSize;
const roundIndex = Math.round(index);
console.log("roundIndex:", roundIndex);
currentItem = roundIndex;
}, []);
useEffect(() => {
async function fetchPosts() {
setLoading(true);
const { data, error } = await supabase
.from("posts")
.select("*")
.order("date", { ascending: false })
.range((page - 1) * PAGE_SIZE, page * PAGE_SIZE - 1);
if (error) {
console.error(error);
showAlert();
setLoading(false);
return;
}
const newPosts = data.map((post: any) => new Post(post));
setPosts((prevPosts) => [...prevPosts, ...newPosts]);
setLoading(false);
setHasMore(data.length === PAGE_SIZE);
}
async function fetchUsers() {
const { data, error } = await supabase.from("posts").select("*");
if (error) {
showAlert();
console.error(error);
return;
}
const newUsers = data.map((user: any) => new User(user));
newUsers.forEach((user) => {
const userPosts = posts.filter((post) => post.uid === user.uid);
user.posts = [...user.posts, ...userPosts];
});
setUsers((prevUsers) => [...prevUsers, ...newUsers]);
}
fetchPosts();
fetchUsers();
}, [page]);
const handleLoadMore = () => {
if (!loading && hasMore) {
setPage((prevPage) => prevPage + 1);
}
};
const handleScroll = (event: any) => {
const index = Math.floor(
Math.floor(event.nativeEvent.contentOffset.x) /
Math.floor(event.nativeEvent.layoutMeasurement.width)
);
currentItem = index;
};
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
{loading ? (
<Text>Loading...</Text>
) : (
<FlatList
data={posts}
horizontal={false}
directionalLockEnabled={true}
renderItem={({ item }) => (
<View>
<HomePost
post={item}
user={
users.filter(function (u) {
return u.uid == item.uid;
})[0]
}
index={posts.indexOf(item)}
loading={loading}
/>
<SizedBox vertical={5} />
</View>
)}
keyExtractor={(item) => item.postId}
onEndReached={handleLoadMore}
onEndReachedThreshold={0.1}
onScroll={onScroll}
onMomentumScrollEnd={onScroll}
/>
)}
</View>
);
}
If you provide a viewabilityConfig to the FlatList, you can use the onViewableItemsChanged event to learn which items are on screen. You just have to make sure that both the viewabilityConfig and onViewableItemsChanged values never change:
import { useState, useEffect, useRef, useCallback } from 'react';
import { Text, View, StyleSheet, FlatList, Image } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
const API_URL = 'https://random-data-api.com/api/v2/users?size=25';
export default function App() {
const [posts, setPosts] = useState([]);
const [visibleItems, setVisibleItems] = useState([]);
// wrapped in ref so that re-renders doesnt recreate it
const viewabilityConfig = useRef({
minimumViewTime: 100,
itemVisiblePercentThreshold: '90%',
}).current;
// wrapped in useCallback so that re-renders doesnt recreate it
const onViewableItemsChanged = useCallback(({ viewableItems }) => {
setVisibleItems(viewableItems.map(({ item }) => item));
}, []);
useEffect(() => {
fetch(API_URL)
.then((data) => data.json())
.then(setPosts);
}, []);
return (
<View style={styles.container}>
{visibleItems.length > 0 && (
<Text>
Currently visible:{' '}
{visibleItems
.map((item) => item.first_name + ' ' + item.last_name)
.join(', ')}
</Text>
)}
<View style={styles.flatlistContainer}>
<FlatList
data={posts}
renderItem={(props) => <Item {...props} />}
viewabilityConfig={viewabilityConfig}
onViewableItemsChanged={onViewableItemsChanged}
/>
</View>
</View>
);
}
const Item = ({ item }) => {
return (
<View style={styles.itemContainer}>
<Text>
{item.first_name} {item.last_name}
</Text>
<Image
source={{ uri: item.avatar }}
style={{ width: 100, height: 100 }}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
flatlistContainer: {
width: '100%',
height: 500,
backgroundColor: 'lightblue',
},
itemContainer: {
justifyContent: 'center',
alignItems: 'center',
margin: 10,
},
});
Demo
You could get the currently viewable items with onViewableItemsChanged where you should get your information.
<FlatList
data={posts}
horizontal={false}
directionalLockEnabled={true}
// this should return an array with following infos
// [{
// item: {key: "key-12"},
// key: "key-12",
// index: 11,
// isViewable: true
// }]
onViewableItemsChanged={({changed, viewableItems}) => console.log(changed, viewableItems)}
....
I've gone through and created a supabase app that resembles your use case and the onViewableItemsChanged approach should work:
import { useContext, useEffect, useState, useRef, useCallback } from 'react';
import {
View,
StyleSheet,
Dimensions,
FlatList,
ActivityIndicator,
} from 'react-native';
import { Text } from 'react-native-paper';
import { SessionContext } from '../../Context';
import { supabase } from '../../initSupabase';
import PostItem from '../../components/PostItem';
const { height } = Dimensions.get('screen');
const totalPostsToGet = 2;
type Post = {
post: {
text: string;
media: {
type: string;
source: string;
};
};
id: number;
created_at: string;
uid: string;
};
export default function PostScreen(props) {
const { session } = useContext(SessionContext);
const [posts, setPosts] = useState<Post[]>([]);
const [page, setPage] = useState(1);
const [isLoading, setIsLoading] = useState(false);
const [hasMore, setHasMore] = useState(true);
const [visibleItems, setVisibleItems] = useState([]);
const viewabilityConfig = useRef({
// minimumViewTime: 100,
itemVisiblePercentThreshold: 50,
}).current;
// wrapped in useCallback so that re-renders doesnt recreate it
const onViewableItemsChanged = useCallback(({ viewableItems }) => {
setVisibleItems(viewableItems.map(({ item }) => item));
}, []);
const handleLoadMore = () => {
if (!isLoading && hasMore) {
setPage((prevPage) => prevPage + 1);
}
};
useEffect(() => {
if (!session) return;
const fetchLastPost = async () => {
const { data, error } = await supabase
.from('posts')
.select('*')
.order('created_at')
.range(0, 1);
return data[0];
};
const fetchPosts = async () => {
setIsLoading(true);
const lastPost = await fetchLastPost();
console.log('last post', lastPost);
const rangeStart = (page - 1) * totalPostsToGet;
const { data, error } = await supabase
.from('posts')
.select('*')
.order('created_at', { ascending: false })
.range(rangeStart, rangeStart + totalPostsToGet - 1);
if (error) {
console.log('error retrieving profile data', error);
}
if (data) {
setPosts((prev) => prev.concat(data));
// I couldnt figure out how PAGE_SIZE could be used to know that the last post was reached
// so I just grab the last post and look to see if its id is in current data
const hasLastPost = Boolean(
data.find((post) => post.id == lastPost.id)
);
setHasMore(!hasLastPost);
}
setIsLoading(false);
};
fetchPosts();
// subscribe to database changes
const subscription = supabase
.channel(`Posts`)
.on(
'postgres_changes',
{
event: '*',
schema: 'public',
table: 'posts',
// filter: `id=eq.${session.user.id}`,
},
(payload) => {
setIsLoading(true);
console.log('Post update');
setPosts((prev) => {
// either push new post or update existing one
const postIndex = prev.findIndex(
(post) => post.id == payload.new.id
);
if (postIndex < 0) return [...prev, payload.new];
else {
const newPosts = [...prev];
newPosts[postIndex] = payload.new;
return newPosts;
}
});
setIsLoading(false);
}
)
.subscribe();
return () => {
supabase.removeChannel(subscription);
};
}, [session, page]);
return (
<View style={styles.container}>
<Text>Currently visible:</Text>
<View style={{ padding: 5, margin: 5 }}>
{visibleItems.map((item) => (
<Text>{item.post.text.substring(0, 30)}</Text>
))}
</View>
<View style={styles.flatlistContainer}>
{isLoading && <ActivityIndicator />}
<FlatList
data={posts}
keyExtractor={(item: Post) => item.id}
horizontal={false}
directionalLockEnabled
onEndReached={handleLoadMore}
renderItem={(props) => <PostItem {...props} />}
viewabilityConfig={viewabilityConfig}
onViewableItemsChanged={onViewableItemsChanged}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
flatlistContainer: {
height: height * 0.45,
},
});
Demo
Take a look at the Intersection Observer API documentation which is an implementation you can use to detect when an element is visible on the screen or not.
Here's a very simple example where the green div is "observed". Whether it is visible or not is marked in state.
Here's a working sandbox
package.json
{
"name": "react18-intersection-observer",
"version": "1.0.0",
"description": "Detect visible screen elemenet using intersection observer",
"keywords": [
"react",
"starter"
],
"main": "src/index.js",
"dependencies": {
"react": "18.2.0",
"react-bootstrap": "^2.7.1",
"react-dom": "18.2.0",
"react-scripts": "^5.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"author": "Wesley LeMahieu"
}
index.js
import { createRoot } from "react-dom/client";
import App from "./App";
const rootElement = document.getElementById("root");
const root = createRoot(rootElement);
root.render(<App />);
app.js
import { useEffect, useRef, useState } from "react";
const App = () => {
const observer = useRef(null);
const observerRef = useRef(null);
const [isVisible, setIsVisible] = useState(false);
const observerCallback = async (e) => {
if (e.length) {
setIsVisible(e[0].isIntersecting);
} else {
setIsVisible(false);
}
};
useEffect(() => {
if (observerRef.current) {
if (observer.current) {
observer.current.disconnect();
observer.current.observe(observerRef.current);
} else {
observer.current = new IntersectionObserver(observerCallback);
observer.current.observe(observerRef.current);
}
}
return () => observer.current.disconnect();
}, []);
useEffect(() => {
if (isVisible) {
alert("Green box visible");
} else {
alert("Grey box visible");
}
console.log(isVisible ? "GREEN BOX VISIBLE" : "GREEN BOX NOT VISIBLE");
}, [isVisible]);
return (
<div style={{ display: "flex" }}>
<div
style={{
backgroundColor: isVisible ? "green" : "red",
width: "10%",
height: "3000px",
}}
>
Visible
</div>
<div style={{ width: "90%" }}>
<div
style={{ backgroundColor: "grey", opacity: 0.6, height: "1000px" }}
>
Other div
</div>
<div
ref={observerRef}
style={{ backgroundColor: "green", opacity: 0.6, height: "1000px" }}
>
Observed Div
</div>
<div
style={{ backgroundColor: "grey", opacity: 0.6, height: "1000px" }}
>
Other div
</div>
</div>
</div>
);
};
export default App;
I am using redux persist with asyncstorage save items in a bookmarks list. The items are in a flatlist and when I click on one item, it navigates me to another screen. I would like to implement the functional bookmark in the header of that screen.
When I tried doing this, and clicked the bookmark in the header, and go back to the bookmarks, it just shows a blank card. It looks like it is not updating the state properly. How can I fix this?
StackNavigator.tsx
const MainStackNavigator = () => {
const { books, bookmarks } = useAppSelector((state) => state.booksReducer);
const dispatch = useDispatch();
const fetchBooks = () => dispatch(getBooks());
const addToBookmarkList = (book) => dispatch(addBookmark(book));
const removeFromBookmarkList = (book) => dispatch(removeBookmark(book));
useEffect(() => {
fetchBooks();
}, []);
const handleAddBookmark = (book) => {
addToBookmarkList(book);
};
const handleRemoveBookmark = (book) => {
removeFromBookmarkList(book);
};
const handleSwapBookmark = (book) => {
removeFromBookmarkList(book);
};
const RenderItem = () => {
const ifExists = (book) => {
if (bookmarks.filter((item) => item.id === book.id).length > 0) {
return true;
}
return false;
};
return (
<TouchableOpacity
onPress={() =>
ifExists(i) ? handleRemoveBookmark(i) : handleAddBookmark(i)
}
activeOpacity={0.7}
style={{
flexDirection: "row",
padding: 2,
backgroundColor: ifExists(i) ? "#F96D41" : "#2D3038",
borderRadius: 20,
alignItems: "center",
justifyContent: "center",
height: 40,
width: 40,
}}
>
<MaterialCommunityIcons
color={ifExists(i) ? "white" : "#64676D"}
size={24}
name={ifExists(i) ? "bookmark-outline" : "bookmark"}
/>
</TouchableOpacity>
);
};
return (
<AppStack.Navigator>
<AppStack.Screen
name="BookmarksScreen"
component={BookmarksScreen}
options={{
title: "Search",
statusBarColor: isDarkMode ? "white" : "black",
headerLargeTitle: true,
headerTranslucent: true,
headerLargeTitleHideShadow: true,
}}
/>
<AppStack.Screen
name="Screen2"
component={Screen2}
options={({ route }) => ({
headerLargeTitle: false,
title: route.params.name,
headerTranslucent: true,
headerRight: () => <RenderItem item={route.params.name} />,
})}
/>
</AppStack.Navigator>
);
};
actions.js
import axios from "axios";
import { BASE_URL } from "../config";
// Define action types
export const GET_BOOKS = "GET_BOOKS";
export const ADD_TO_BOOKMARK_LIST = "ADD_TO_BOOKMARK_LIST";
export const REMOVE_FROM_BOOKMARK_LIST = "REMOVE_FROM_BOOKMARK_LIST";
export const SWAP_IN_BOOKMARK_LIST = "SWAP_IN_BOOKMARK_LIST";
export const getBooks = () => {
try {
return async (dispatch) => {
const response = await axios.get(`${BASE_URL}`);
if (response.data) {
dispatch({
type: GET_BOOKS,
payload: response.data,
});
} else {
console.log("Unable to fetch data from the API BASE URL!");
}
};
} catch (error) {
console.log(error);
}
};
export const addBookmark = (book) => (dispatch) => {
dispatch({
type: ADD_TO_BOOKMARK_LIST,
payload: book,
});
};
export const removeBookmark = (book) => (dispatch) => {
dispatch({
type: REMOVE_FROM_BOOKMARK_LIST,
payload: book,
});
};
hooks.ts
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import type { RootState, AppDispatch } from "./store";
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
reducers.ts
import {
GET_BOOKS,
ADD_TO_BOOKMARK_LIST,
REMOVE_FROM_BOOKMARK_LIST,
} from "./actions";
const initialState = {
books: [],
bookmarks: [],
};
function booksReducer(state = initialState, action) {
switch (action.type) {
case GET_BOOKS:
return { ...state, books: action.payload };
case ADD_TO_BOOKMARK_LIST:
return { ...state, bookmarks: [...state.bookmarks, action.payload] };
case REMOVE_FROM_BOOKMARK_LIST:
return {
...state,
bookmarks: state.bookmarks.filter(
(book) => book.id !== action.payload.id
),
};
default:
return state;
}
}
export default booksReducer;
store.ts
import { createStore, combineReducers, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import AsyncStorage from "#react-native-async-storage/async-storage";
import { persistStore, persistReducer } from "redux-persist";
import booksReducer from "./reducers";
const persistConfig = {
key: "root",
storage: AsyncStorage,
whitelist: ["bookmarks"],
};
const rootReducer = combineReducers({
booksReducer: persistReducer(persistConfig, booksReducer),
});
export const store = createStore(rootReducer, applyMiddleware(thunk));
export const persistor = persistStore(store);
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
BookmarksScreen.tsx
const BookmarksScreen = () => {
return (
<View>
<FlatList
data={bookmarks}
keyExtractor={(item) => item.id}
renderItem={renderItem}
showsVerticalScrollIndicator={false}
/>
</View>
);
}
renderItem
const renderItem = ({ item }) => {
return (
<TouchableOpacity
onPress={() =>
navigation.navigate("Screen2", {name: item.name})}
>
<View style={{ flexDirection: "row", flex: 1 }}>
<View>
<Text
style={{
fontSize: 22,
paddingRight: 16,
color: "black",
fontFamily: "Medium",
left: 45,
top: 6,
}}
>
{item.country}
</Text>
</View>
</View>
</View>
</TouchableOpacity>
);
};
I think you forgot to access props that's the reason booked-marked not working i change some code please check it's working or not.
StackNavigator.tsx
const RenderItem = (item) => {
const ifExists = (book) => {
if (bookmarks.filter((item) => item.id === book.id).length > 0) {
return true;
}
return false;
};
return (
<TouchableOpacity
onPress={() =>
ifExists(item) ? handleRemoveBookmark(item) : handleAddBookmark(item)
}
activeOpacity={0.7}
style={{
flexDirection: "row",
padding: 2,
backgroundColor: ifExists(i) ? "#F96D41" : "#2D3038",
borderRadius: 20,
alignItems: "center",
justifyContent: "center",
height: 40,
width: 40,
}}
>
<MaterialCommunityIcons
color={ifExists(i) ? "white" : "#64676D"}
size={24}
name={ifExists(i) ? "bookmark-outline" : "bookmark"}
/>
</TouchableOpacity>
);
};
I am creating an app which uses the phone's location. I would like to be able to get the latitude and longitude and using it as part of an api address.
I have been following this sample code using react-native-get-location and have been able to print the information in json formate but can't pull the latitude and longitude and use them.
react-native-get-location
Here is my code.
import GetLocation from 'react-native-get-location'
export default class App extends React.Component {
constructor (props) {
super(props);
this.state = {
isLoading: true,
latitude: null,
longitude: null,
location: null
};
}
_requestLocation = () => {
GetLocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 150000,
})
.then(location => {
this.setState ({
location,
isLoading: false,
});
})
.catch(error => {
const { code, message} = error;
if (code === 'CANCELLED') {
Alert.alert('location cancelled by user or by another request');
}
if (code === 'UNAVAILABLE') {
Alert.alert('Location service is disabled or unavailable');
}
if (code === 'TIMEOUT') {
Alert.alert('Location request timed out');
}
if (code === 'UNAUTHORIZED') {
Alert.alert('Authorization denied')
}
this.setState({
location: null,
isLoading: false,
});
});
}
componentDidMount() {
GetLocation.getCurrentPosition(async (info) => {
const location = await GetLocation(
info.coords.latitude,
info.coords.longitude
);
})
const fetch = require('node-fetch');
fetch('https://api.weatherapi.com/v1/forecast.json?&q=London', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
}).then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
this.setState({
isLoading: false,
dataSource: responseJson,
})
}).catch((error) => {
console.error(error);
});
}
render() {
const {location, isLoading} = this.state;
if (this.state.isLoading) {
return (
<View style={{flex: 1, paddingTop: 20}}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{flex:1, paddingTop: 20}}>
<Text>{JSON.stringify(location, 0, 2)}</Text>
<View style={{flex:1, flexDirection: 'row', textAlign: 'center', paddingLeft: 90}}>
<Button
disabled={isLoading}
title="Get Location"
onPress={this._requestLocation}
/>
</View>
</View>
)
}
}
Use expo-location instead of react-native-get-location as it is very easy to implement.
Here is the working app: Expo Snack
Screenshot:
import React, { useEffect, useState } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
let apiKey = 'YOUR_API_KEY';
import * as Location from 'expo-location';
export default function App() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const [address, setAddress] = useState(null);
// const [getLocation, setGetLocation] = useState(false);
const getLocation = () => {
(async () => {
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
}
Location.setGoogleApiKey(apiKey);
console.log(status);
let { coords } = await Location.getCurrentPositionAsync();
setLocation(coords);
console.log(coords);
if (coords) {
let { longitude, latitude } = coords;
let regionName = await Location.reverseGeocodeAsync({
longitude,
latitude,
});
setAddress(regionName[0]);
console.log(regionName, 'nothing');
}
// console.log();
})();
};
return (
<View style={styles.container}>
<Text style={styles.big}>
{!location
? 'Waiting'
: `Lat: ${location.latitude} \nLong: ${
location.longitude
} \n${JSON.stringify(address?.['subregion'])}`}
</Text>
<TouchableOpacity onPress={getLocation}>
<View
style={{
height: 100,
backgroundColor: 'teal',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 10,
marginTop: 20,
}}>
<Text style={styles.btnText}> GET LOCATION </Text>
</View>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
big: {
fontSize: 18,
color: 'black',
fontWeight: 'bold',
},
btnText: {
fontWeight: 'bold',
fontSize: 25,
color: 'white',
},
});
react-native-geolocation-service is a good alternative too for fetching latitude & longitude values.
Example usage:
import GeoLocation from 'react-native-geolocation-service';
const getDeviceCurrentLocation = async () => {
return new Promise((resolve, reject) =>
GeoLocation.getCurrentPosition(
(position) => {
resolve(position);
},
(error) => {
reject(error);
},
{
enableHighAccuracy: true, // Whether to use high accuracy mode or not
timeout: 15000, // Request timeout
maximumAge: 10000 // How long previous location will be cached
}
)
);
};
I'm trying to add an offline notification bar into my app, I have the following code that is called from my App.js.
import React, { PureComponent } from 'react';
import { View, Text, Dimensions, StyleSheet,Alert } from 'react-native';
import NetInfo from "#react-native-community/netinfo";
const dimensions = Dimensions.get('window');
let outofaction = 1;
NetInfo.fetch().then(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
if (state.isConnected == false) {
outofaction = 0;
} else {
outofaction = 1;
}
});
//class OfflineNotice extends PureComponent {
const OfflineNotice = () => {
NetInfo.fetch().then(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
if (state.isConnected == false) {
outofaction = 0;
} else {
outofaction = 1;
}
});
// Subscribe
const unsubscribe = NetInfo.addEventListener(state => {
console.log("Connection type", state.type);
console.log("Is connected?", state.isConnected);
if (state.isConnected == false) {
outofaction = 0;
} else {
outofaction = 1;
}
});
// Unsubscribe
unsubscribe();
function MiniOfflineSign() {
if (outofaction == 0) {
return (
<View style={styles.offlineContainer}>
<Text style={styles.offlineText}>Offline</Text>
</View>
);
} else {
return (
<View style={styles.offlineContainer}>
<Text style={styles.offlineText}>Online</Text>
</View>
);
}
}
return (
<MiniOfflineSign />
)};
const styles = StyleSheet.create({
offlineContainer: {
backgroundColor: '#b52424',
height: 30,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
width: dimensions.width,
position: 'absolute',
top:40,
zIndex:1
},
offlineText: { color: '#fff' }
});
export default OfflineNotice;
The code works partially. I start Online, then turn off my internet on my laptop, and if I refresh / reload, then it will show offline.
Two problems I have;
I want it to update in near real-time when the isConnected changes (this doesn't appear to be happening)
It doesn't get stuck on one state (though item 1 above would fix that)
I made something similar, this might help you with things.
NoInternetMessageBar component
// NoInternetMessageBar.js
import Netinfo from '#react-native-community/netinfo';
import React, { useContext, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Store } from 'store/Store';
export default function NoInternetMessageBar() {
const {
state,
actions: { networkConnectionChanged },
} = useContext(Store);
useEffect(() => {
const unsubscribe = Netinfo.addEventListener(({ isConnected }) => networkConnectionChanged(isConnected));
return () => {
unsubscribe();
};
}, []);
if (state.isConnected) return null;
return (
<View style={styles.container}>
<Text style={styles.message}>Cannot reach internet</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
position: 'absolute',
bottom: 0,
height: 40,
width: '100%',
backgroundColor: 'gray',
},
message: {
color: 'white',
marginLeft: 20,
},
});
action
const networkConnectionChanged = async isConnected => {
return dispatch({
type: NETWORK_CHANGED,
payload: isConnected,
});
};
reducer
case NETWORK_CHANGED:
return {
...state,
isConnected: action.payload,
};
On top level use it like this.
<>
<AppContainer />
<NoInternetMessageBar />
</>
You are unsubscribing from NetInfo updates immediately after subscribing to them. Functional components are just that - functions. The code you wrote in there will be executed every time that component renders.
You should instead put your subscribe/unsubscribe in a useEffect hook, so that you subscribe on mount and unsubscribe on unmount.
useEffect(() => {
const unsubscribe = NetInfo.addEventListener(state => {
...
});
return () => {
unsubscribe();
}
}, []);
Subscribe in componentDidMount() and Unsubscribe in componentWillUnmount(), like this -
class A extends React.Component {
constructor() {
this.unsubscribe;
}
componentDidMount(){
this.unsubscribe = NetInfo.addEventListener(...);
}
componentWillUnmount() {
this.unsubscribe && this.unsubscribe();
}
}
This may sound duplicated, but it's not.
I'm kinda newbie in React-Native.
I'm trying to write an MQTT-Client app, making use of "react-native-paho-mqtt" library, which should be able to connect to my own broker with this uri: tcp://217.218.171.92:1883
but apparently the uri must be started with "wss" only!!!
How can I make my app use tcp:// ?
above that, is it possible??
here is my App.js:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TouchableHighlight,
TextInput,
Button
} from 'react-native';
import { Client, Message } from 'react-native-paho-mqtt';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
export default class App extends Component<{}> {
constructor(){
super();
const myStorage = {
setItem: (key, item) => {
myStorage[key] = item;
},
getItem: (key) => myStorage[key],
removeItem: (key) => {
delete myStorage[key];
},
};
const client = new Client({ uri: "wss://m12.cloudmqtt.com:31839/", clientId: 'clientIdReactNative' + (new Date()).getTime(), storage: myStorage });
client.on('messageReceived', (entry) => {
console.log(entry);
this.setState({message: [...this.state.message, entry.payloadString]});
});
client.on('connectionLost', (responseObject) => {
if (responseObject.errorCode !== 0) {
console.log(responseObject.errorMessage);
this.setState({error: 'Lost Connection', isConnected: false});
}
});
this.connect(client)
.then(() => {
console.log('connect!');
this.setState({isConnected: true, error: ''})
})
.catch((error)=> {
console.log(error);
});
this.state = {
client,
message: [''],
messageToSend:'',
isConnected: false
}
}
connect(client){
return client.connect({
useSSL: true,
userName: 'azebvdny',
password: 'MsULac9Uhig0'
})
.then(() => {
client.subscribe('S/ReactMQTT');
})
}
onConnect = () => {
const { client } = this.state;
client.subscribe('ReactMQTT');
this.pushText('connected');
};
pushText = entry => {
const { message } = this.state;
this.setState({ message: [...message, entry] });
};
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
componentWillMount(){
}
sendMessage(){
var message = new Message(this.state.messageToSend);
message.destinationName = "S/ReactMQTT";
if(this.state.isConnected){
this.state.client.send(message);
}else{
this.connect(this.state.client)
.then(() => {
console.log('connect!');
this.state.client.send(message);
this.setState({error: '', isConnected: true});
})
.catch((error)=> {
console.log(error);
this.setState({error: error});
});
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Nativess!
</Text>
<Text style={styles.instructions}>
Message: {this.state.message.join(' --- ')}
</Text>
<Text style={{color: 'red'}}>
{this.state.error}
</Text>
{ this.state.isConnected ?
<Text style={{color: 'green'}}>
Connected
</Text> : null
}
<TextInput
value={this.state.messageToSend}
onChangeText={(value => this.setState({messageToSend: value}))}
placeholder="Type here... "
style={styles.input} />
<Button onPress={this.sendMessage.bind(this)} title="Send Message" />
</View>
);
}
}
const styles = StyleSheet.create({
...
});
Any help would be greatly appreciated.
react-native-paho-mqtt only supports WebSocket.
because react-native-paho-mqtt doesn't support raw TCP out of the box
If you use real-native-tcp to configure the client, it may be possible on the code.
The example of a module tells us it's not possible.
test('should fail to create a new client with an invalid ws uri', function () {
let client = null;
let error;
try {
client = new Client({ uri: 'http://example.com', clientId: 'testclientid', webSocket, storage });
} catch (err) {
error = err;
}
expect(client).toBe(null);
expect(error).not.toBe(null);
For new googlers: you can use react_native_mqt.
After you managed to properly install the library, for example, this could be your App.js:
import React, { Component } from 'react';
import init from 'react_native_mqtt';
import { AsyncStorage,
StyleSheet,
Text,
View,
TextInput,
Button,
Alert
} from 'react-native';
init({
size: 10000,
storageBackend: AsyncStorage,
defaultExpires: 1000 * 3600 * 24,
enableCache: true,
sync: {},
});
export default class App extends Component {
constructor(){
super();
this.onMessageArrived = this.onMessageArrived.bind(this)
this.onConnectionLost = this.onConnectionLost.bind(this)
const client = new Paho.MQTT.Client('yourURL', yourPort, 'someClientID',);
client.onMessageArrived = this.onMessageArrived;
client.onConnectionLost = this.onConnectionLost;
client.connect({
onSuccess: this.onConnect,
useSSL: false ,
userName: 'yourUser',
password: 'yourPass',
onFailure: (e) => {console.log("here is the error" , e); }
});
this.state = {
message: [''],
client,
messageToSend:'',
isConnected: false,
};
}
onMessageArrived(entry) {
console.log("onMessageArrived:"+message.payloadString);
this.setState({message: [...this.state.message, entry.payloadString]});
}
onConnect = () => {
const { client } = this.state;
console.log("Connected!!!!");
client.subscribe('hello/world');
this.setState({isConnected: true, error: ''})
};
sendMessage(){
message = new Paho.MQTT.Message(this.state.messageToSend);
message.destinationName = "hello/world";
if(this.state.isConnected){
this.state.client.send(message);
}else{
this.connect(this.state.client)
.then(() => {
this.state.client.send(message);
this.setState({error: '', isConnected: true});
})
.catch((error)=> {
console.log(error);
this.setState({error: error});
});
}
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
this.setState({error: 'Lost Connection', isConnected: false});
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native MQTT!
</Text>
<Text style={styles.instructions}>
Message: {this.state.message.join(' --- ')}
</Text>
<Text style={{color: 'red'}}>
{this.state.error}
</Text>
{ this.state.isConnected ?
<Text style={{color: 'green'}}>
Connected
</Text> : null
}
<TextInput
value={this.state.messageToSend}
onChangeText={(value => this.setState({messageToSend: value}))}
placeholder="Type hereee..."
style={styles.input} />
<Button onPress={this.sendMessage.bind(this) } title="Send Message" />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button: {
padding: 10,
alignItems: 'center',
justifyContent: 'center',
},
buttonLabel: {
color: 'blue',
},
input:{
width: 300
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
Replace the URL and other stuff with your own. For example, you have to put it like this:
const client = new Paho.MQTT.Client('something.something.ir', 1883, '123456');