React Developer
Hi peeps, I am trying to create realtime updates and I am having websocket error
Code is below this msg
Votes
0
Replies
24
Participants
3
Messages
25
React Developer
Hi peeps, I am trying to create realtime updates and I am having websocket error
Code is below this msg
React Developer
lib/appwrite.js
import 'react-native-url-polyfill/auto';import { Client, Account, Avatars, TablesDB, Realtime, Channel } from 'appwrite';
export const client = new Client() .setEndpoint('https://sgp.cloud.appwrite.io/v1') .setProject('I have my project key here');
export const account = new Account(client)export const avatars = new Avatars(client)export const databases = new TablesDB(client)export const realtime = new Realtime(client)React Developer
contents/BooksContext.jsx
import { createContext, useEffect, useState } from "react";import { client, databases, realtime } from "../lib/appwrite";import { ID, Permission, Role, Query, Channel } from "appwrite";import { useUser } from "../hooks/useUser";
export const BooksContext = createContext()
const DATABASE_ID = "69e4a2ec001a2dfb9a35"const COLLECTION_ID = "69e4a312003c069c9a8c"
export function BooksProvider({ children }) { const [books, setBooks] = useState([]) const { user } = useUser()
async function fetchBooks() { try { const response = await databases.listRows({ databaseId: DATABASE_ID, tableId: COLLECTION_ID, queries: [ Query.equal('userId', user.$id) ], })
setBooks(response.rows) console.log(response.rows) } catch (error) { console.log(error) } }
async function createBook(data) { try { const newBook = await databases.createRow({ databaseId: DATABASE_ID, tableId: COLLECTION_ID, rowId: ID.unique(), data: { ...data, userId: user.$id }, $permissions: [ Permission.read(Role.user(user.$id)), Permission.update(Role.user(user.$id)), Permission.delete(Role.user(user.$id)) ]
}) } catch (error) { console.log(error) } }
React Developer
useEffect(() => { let subscription = null; let isMounted = true;
const setupRealtime = async () => { if (user) { fetchBooks();
const channel = Channel.tablesdb(DATABASE_ID).table(COLLECTION_ID).row().create();
const sub = await realtime.subscribe(channel, (response) => { if (isMounted) { setBooks((prevBooks) => [...prevBooks, response.payload]); } });
if (!isMounted) { await sub.close(); } else { subscription = sub; } } else { setBooks([]); } };
setupRealtime();
return () => { isMounted = false; if (subscription) { subscription.close(); } }; }, [user]);
return ( <BooksContext.Provider value={{ books, fetchBooks, fetchBookById, createBook, deleteBook }}> {children} </BooksContext.Provider> )}React Developer
Error:
Call Stack construct (<native>) apply (<native>) _construct (node_modules\@babel\runtime\helpers\construct.js) Wrapper (node_modules\@babel\runtime\helpers\wrapNativeSuper.js) construct (<native>) _callSuper (node_modules\@babel\runtime\helpers\callSuper.js) NamelessError (node_modules\@expo\metro-runtime\src\metroServerLogs.native.ts) captureCurrentStack (node_modules\@expo\metro-runtime\src\metroServerLogs.native.ts) HMRClient.log (node_modules\@expo\metro-runtime\src\metroServerLogs.native.ts) console.level (node_modules\react-native\Libraries\Core\setUpDeveloperTools.js) socket.addEventListener$argument_1 (node_modules\appwrite\dist\esm\sdk.js) call (<native>) invoke (node_modules\react-native\src\private\webapis\dom\events\EventTarget.js) dispatch (node_modules\react-native\src\private\webapis\dom\events\EventTarget.js) dispatchEvent (node_modules\react-native\src\private\webapis\dom\events\EventTarget.js) _eventEmitter.addListener$argument_1 (node_modules\react-native\Libraries\WebSocket\WebSocket.js) apply (<native>) emit (node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js) apply (<native>) <anonymous> (node_modules\@babel\runtime\helpers\superPropGet.js) RCTDeviceEventEmitterImpl#emit (node_modules\react-native\Libraries\EventEmitter\RCTDeviceEventEmitter.js) ERROR Failed to reconnect: [Error: WebSocket error] React Developer
creating new row is working just fine
React Developer
its just realtime is killing itself
React Developer
Websocket error Realtime
React Developer
please ping me when you reply
Weird -> Nothing is pointing towards the appwrite SDK 🤔
Pointing towards expo lib
Any issues in the setup of the expo?
Rank 1: Thread
https://youtu.be/J2j1yk-34OY?si=WRuGsJx6KvfoZtap
this is how I did the setup of my expo project
Rank 1: Thread
https://youtu.be/eMbtOh17RuQ?si=A8NpdrYcg2ec6Qw2
and this for backend
Rank 1: Thread
This is how I created the project
npx create-expo-app@latest --template blank@sdk-55
Rank 1: Thread
can you tell me steps which I can do to show you the specific errors??
I am also thinking, I dont have much prior experience with react native
@Chirag Aggarwal any ideas?
not much idea either
can you isolate things a bit? Instead of setuping realtime in the whole application, try setting realtime in a single component
Then try to find -> whether it is actually connecting or not
Might be some network connection issue
AFAIK, react native must have a network toolbar to debug as well
Rank 1: Thread
I am the office right now, will do check and let you know as soon as I can
Rank 1: Thread
just wondering, is it some limitations of Expo Go? since It's a sandbox environment.
Do I have to build the app and install as a normal app?
TBH, I am not sure
There is something expo development mode or something you could try
Can u try to isolate the error ?
Rank 1: Thread
I'm sorry for asking too much, it's just I'm new to react native and appwrite.
npx expo prebuildnpx expo run:androidThis will run it as a development mode right?
I meant this
Create a development build on EAS - Expo Documentation https://share.google/7upbm7pcDQ0XiAiTn
Rank 1: Thread
sure will do that the moment I get time
Rank 1: Thread
and again really sorry if I'm asking too many questions, it's just I like to get everything properly understood
No worries, it would also help us find the bugs as well if any