CodingWithJamal
CodingWithJamal
CCConvex Community
Created by CodingWithJamal on 11/15/2024 in #support-community
How to handle the elapse of time for debounce logic in Convex?
I’m building the notification logic for direct messaging. My app is prelaunch, and I want to keep the notification logic simple for the MVP. For now, the logic should be based on data that's simple to gather, e.g. debounce time and read receipts. In the future, we may add more factors for the logic, e.g. user presence. And more notification types, e.g. native in-app notifications. The key challenge I’m facing in Convex is managing time-based logic to debounce email notifications. Ideally, the app delays notifications for a short period in order to group multiple messages into a single email. Below are my current potential approaches for debouncing. Which do you recommend? A) Convex actions to handle server-side logic for delayed email sending. My concern is whether actions are the right tool for managing time-based workflows and how they interact with Convex’s persistence model. B) Convex scheduled functions to periodically check for new notifications and batch-send them to the client. C) Convex Workflow component to orchestrate long-running processes, ensuring that the debounce logic executes reliably over time. I'm considering defining a workflow that delays email sending to batch multiple messages. D) Some other approach?
30 replies
CCConvex Community
Created by CodingWithJamal on 11/15/2024 in #support-community
Notification System Design Advice
No description
6 replies
CCConvex Community
Created by CodingWithJamal on 9/26/2024 in #support-community
Need a little react help
Hello family, its been a while. So for my site I wanted to save a simple count of users visited. I wrote this code:
"use client";

import { useMutation } from "convex/react";
import { useEffect } from "react";
import { api } from "../../convex/_generated/api";
import useLocalStorage from "@/hooks/useLocalStorage";

const GlobalStatsTracker = () => {
const [viewCounted, setViewCounted] = useLocalStorage<"no" | "yes">(
"view_counted",
{
defaultValue: "no",
}
);
const updateGlobalStats = useMutation(api.global_kv.updateGlobalStats);

useEffect(() => {
console.table({
viewCounted,
"window.location.host.includes('localhost')":
window.location.host.includes("localhost"),
"typeof window === 'undefined'": typeof window === "undefined",
});

if (typeof window === "undefined") return;
if (viewCounted === "yes") return;
if (process.env.NODE_ENV === "development") return;

updateGlobalStats({ total_users_visited: true })
.then(() => {
setViewCounted("yes");
})
.catch(console.error);
}, [viewCounted]);

return null;
};

export default GlobalStatsTracker;
"use client";

import { useMutation } from "convex/react";
import { useEffect } from "react";
import { api } from "../../convex/_generated/api";
import useLocalStorage from "@/hooks/useLocalStorage";

const GlobalStatsTracker = () => {
const [viewCounted, setViewCounted] = useLocalStorage<"no" | "yes">(
"view_counted",
{
defaultValue: "no",
}
);
const updateGlobalStats = useMutation(api.global_kv.updateGlobalStats);

useEffect(() => {
console.table({
viewCounted,
"window.location.host.includes('localhost')":
window.location.host.includes("localhost"),
"typeof window === 'undefined'": typeof window === "undefined",
});

if (typeof window === "undefined") return;
if (viewCounted === "yes") return;
if (process.env.NODE_ENV === "development") return;

updateGlobalStats({ total_users_visited: true })
.then(() => {
setViewCounted("yes");
})
.catch(console.error);
}, [viewCounted]);

return null;
};

export default GlobalStatsTracker;
which works fine but for some reason every page refresh the updateGlobalStats is called even when the view has been set in local storage? This should not happen
viewCounted 'yes'
window.location.host.includes('localhost') true
typeof window === 'undefined' false
viewCounted 'yes'
window.location.host.includes('localhost') true
typeof window === 'undefined' false
This is the console output.
12 replies
CCConvex Community
Created by CodingWithJamal on 7/24/2024 in #support-community
Realtime game with convex...?
So for the last few days ive been using socket.io (in rust) to build a simple 2d realtime game in react. I didnt want to use raw ws api but I also wanted control over how my data is sent. But then it made me think, could I use convex as a point of truth for my game since all clients can connect to the same query and mutate data? In my rust backend server I have the server create a room id, and spawn threads for players for each room. However in convex I could just have tables and when a game is created just add the userId to an active_game table or something. But another problem I thought about is player movements. The only way to make things seem realtime is to have updates to all player actions brodcasted to other players as well. This feels very expensive as players can trigger thoughsands of mutations per minute when playing the game... Maybe I could get away with coding a complex backend myself If I self host convex? Anyways, I really just want the community's opinion on this idea. Convex is great as a database and its selling point is realtime connections. If I was building a chess clone, this would be easier but I want a realtime game were players can move and control there 2d character and other players connected to the same game can see there movements. Which might be were my limitation is reached with convex... Either way, If I dont use convex as my core backend and write my own..I will still use convex for authenication and other site features later on for the game.
19 replies
CCConvex Community
Created by CodingWithJamal on 7/16/2024 in #support-community
Convex Auth Discord Setup
No description
10 replies
CCConvex Community
Created by CodingWithJamal on 6/18/2024 in #support-community
Convex ents
Hey, ive been playing with using convex ents today and like it. I didnt see an answer in the docs so im here... I like the idea of https://labs.convex.dev/convex-ents/schema#unique-fields , they can be helpful for things like an email field. however, when a uniue field is found, how does the convex function tell me this? I dont see any error type info on ents. I need a way to tell my froend end about this,
6 replies
CCConvex Community
Created by CodingWithJamal on 3/29/2024 in #support-community
Help moving data to prod
So for my convex app, i have some data in dev that ive been using to built out my site. I want to reuse this data in prod again without having to run more web crawlers just to get the data... Is there a good way todo this already?
5 replies
CCConvex Community
Created by CodingWithJamal on 3/25/2024 in #support-community
Convex Rust Client (None Async) help
Hello, im writing a a web scrapper and i want to save some data to convex. I cant use async rust. So how can i use the convex base client to send some mutation data over to my convex server.
const MUTATION_QUERY_KEY: &str = "anime:createAnimeData";

fn save_to_database() {
println!("Loading data from disk...");

let mut data: Vec<AnimeData> = Vec::new();
let files = std::fs::read_dir("anime").unwrap();

for file in files {
let path = file.unwrap().path();
if path.is_file() {
let contents = std::fs::read_to_string(path).unwrap();
let json_data: AnimeData = serde_json::from_str(&contents).unwrap();
data.push(json_data);
}
}

println!("Sending data to convex server...");

let client = BaseConvexClient::new();

for anime in data {
client.mutation(MUTATION_QUERY_KEY, maplit::btreemap! {});
}

println!("Done!");
}
const MUTATION_QUERY_KEY: &str = "anime:createAnimeData";

fn save_to_database() {
println!("Loading data from disk...");

let mut data: Vec<AnimeData> = Vec::new();
let files = std::fs::read_dir("anime").unwrap();

for file in files {
let path = file.unwrap().path();
if path.is_file() {
let contents = std::fs::read_to_string(path).unwrap();
let json_data: AnimeData = serde_json::from_str(&contents).unwrap();
data.push(json_data);
}
}

println!("Sending data to convex server...");

let client = BaseConvexClient::new();

for anime in data {
client.mutation(MUTATION_QUERY_KEY, maplit::btreemap! {});
}

println!("Done!");
}
Here is the code. However i have a few errors:
mismatched types
expected `UdfPath`, found `&str`
mismatched types
expected `UdfPath`, found `&str`
Why does the mutation take this type? Should it not be the key of the function?
7 replies
CCConvex Community
Created by CodingWithJamal on 3/23/2024 in #support-community
RSS in convex help
Good afteroon, so for my site I want to implement https://www.npmjs.com/package/rss-parser and add some data to my nextjs app. im trying to think of a way to save the feed data into convex, but how would i use the library on the backend? The only way i know to run convex code like db.insert is in functions but we can use npm. im trying to avoid having to write another rest server just to save rss information. Would i use maybe nextjs api rountes?
17 replies
CCConvex Community
Created by CodingWithJamal on 2/9/2024 in #support-community
React Native / Clerk Auth / Expo Help
12 replies
CCConvex Community
Created by CodingWithJamal on 12/5/2023 in #support-community
Convex dev hanging
No description
8 replies
CCConvex Community
Created by CodingWithJamal on 11/16/2023 in #support-community
NextJs Server Actions + Convex not working
Im trying to use convex http client in nextjs server actions to submit data. However its not working. When i console log the form data, i get all my valid data. However the mutation does not submit to convex? It does not run at all and theres no errors i can see.
'use server';

import { convexClient } from '@/lib/convex';
import { api } from '../../convex/_generated/api';

export async function subscribeToWaitingList(formData: FormData) {
try {
let email = formData.get('email') as string | undefined;

if (!email) throw new Error('Email is required');

const check = await convexClient.query(api.waiting_list.alreadySubscribed, {
email,
})

if(check === "subscribed") {
return {
success: false,
error: 'You are already subscribed to the waiting list',
}
}

await convexClient.mutation(api.waiting_list.create, {
email,
});

return {
success: true,
error: null,
}
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
}
'use server';

import { convexClient } from '@/lib/convex';
import { api } from '../../convex/_generated/api';

export async function subscribeToWaitingList(formData: FormData) {
try {
let email = formData.get('email') as string | undefined;

if (!email) throw new Error('Email is required');

const check = await convexClient.query(api.waiting_list.alreadySubscribed, {
email,
})

if(check === "subscribed") {
return {
success: false,
error: 'You are already subscribed to the waiting list',
}
}

await convexClient.mutation(api.waiting_list.create, {
email,
});

return {
success: true,
error: null,
}
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
}
17 replies
CCConvex Community
Created by CodingWithJamal on 11/2/2023 in #support-community
Database Architecture Concerns
Hello convex team. Currently im working on a new SAAS application and its main feature allows users to post data to my public api and store data for them to view later. In theory i want users to be able to store thousands of rows in my database for them to query later, however I was thinking would this not be bad for convex later on? Because each users data does not need to be synced with each other (always). Idealy, I would want a master database where the auth user data is stored, and other things. But users specific data is stored in there own database (table in this case) where each user (s) can only see there data. Im not sure if convex can do something like this, im concerned that in the future If i have a lot of data, it will slow down other users accessing data because the data stored will still need to be parsed from top to bottom in the querys. Just looking for some advice, I love convex as a datavase but im not sure If its the best at my needs for this project. Vs something like sqlite where If i wanted more databases, I could just make another file and have user data separated from each other completely and not have other user data effecting performance (reads / writes ) of others
3 replies
CCConvex Community
Created by CodingWithJamal on 10/30/2023 in #support-community
Convex Argument Validtion Error?
> [CONVEX Q(projects:list)] ArgumentValidationError: Object is missing the required field `user_id`. Consider wrapping the field validator in `v.optional(...)` if this is expected.

Object: {}
Validator: v.object({user_id: v.string()})
> [CONVEX Q(projects:list)] ArgumentValidationError: Object is missing the required field `user_id`. Consider wrapping the field validator in `v.optional(...)` if this is expected.

Object: {}
Validator: v.object({user_id: v.string()})
I keep getting this error. Im not sure why?
export const list = query({
args: {
user_id: v.string(),
},
handler: async (ctx, { user_id }) => {
const user = await getUserWithId(ctx, user_id);

if (!user) return [];

const projects = [];

for (const id of user.projects) {
const project = await ctx.db.get(id);
if (project) {
projects.push(project);
}
}

return projects;
},
});
export const list = query({
args: {
user_id: v.string(),
},
handler: async (ctx, { user_id }) => {
const user = await getUserWithId(ctx, user_id);

if (!user) return [];

const projects = [];

for (const id of user.projects) {
const project = await ctx.db.get(id);
if (project) {
projects.push(project);
}
}

return projects;
},
});
4 replies
CCConvex Community
Created by CodingWithJamal on 10/27/2023 in #support-community
React Server Components + Convex Auth State Support?
So im in my nextjs app the convex docs says to use the useConvexAuth hook.
const { isAuthenticated} = useConvexAuth();
const { isAuthenticated} = useConvexAuth();
hook makes sure that the browser has fetched the auth token needed to make authenticated requests to your Convex backend
However, you cant use Hooks in RSC, so is it not possible for convex to know the auth state on the server side? Im asking because i want to fetch data on the server side and pass it to my client components as props. Avoiding having to do a lot of api calls from the client side. I know I can use the Js http client but im not sure how to handle auth session from there. And I want most of my business logic to stay on the server.
9 replies
CCConvex Community
Created by CodingWithJamal on 9/5/2023 in #support-community
Stripe and Convex HTTP Help
No description
15 replies