Back

Appwrite Cloud Function - createSubscriber EndPoint not working

  • 0
  • Functions
  • Cloud
Zionnite
13 Jun, 2024, 08:35

Hello appwriter & team i'm trying to subscribe my already existing users to a topic but its not working, it just get stuck at a place

TypeScript

// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }) => {
  const client = new Client();
  client.setEndpoint(process.env.MY_APPWRITE_ENDPOINT).setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID).setKey(process.env.APPWRITE_API_KEY);
  const functions = new Functions(client);
  const database = new Databases(client);
  const storage = new Storage(client);
  const users = new Users(client);


  const ENDPOINT          = process.env.MY_APPWRITE_ENDPOINT;
  const PROJECT_ID        = process.env.APPWRITE_FUNCTION_PROJECT_ID;
  const API_KEY           = process.env.APPWRITE_API_KEY;
  const DATABASE_ID       = process.env.DATABASE_ID;
  const USERS_ID          = process.env.USERS_ID;
  const SMS_PROVIDER_ID             = process.env.SMS_PROVIDER_ID;
  const EMAIL_PROVIDER_ID           = process.env.EMAIL_PROVIDER_ID;
  const PUSH_PROVIDER_ID            = process.env.PUSH_PROVIDER_ID;

  const GENERAL_TOPIC_ALL             = process.env.GENERAL_TOPIC_ALL;
  const GENERAL_TOPIC_SMS             = process.env.GENERAL_TOPIC_SMS;
  const GENERAL_TOPIC_PUSH            = process.env.GENERAL_TOPIC_PUSH;
  const GENERAL_TOPIC_EMAIL           = process.env.GENERAL_TOPIC_EMAIL;

const data    = JSON.parse(req.bodyRaw);  
  const subscribeType     = data.type;

  try{
TL;DR
Developers are experiencing issues with the createSubscriber endpoint in the Appwrite Cloud Function. The code appears to be getting stuck at a certain point. The issue might be due to handling multiple subscriber creations. They should check the logical flow for each subscribeType and refine the code accordingly, ensuring each messaging.createSubscriber call happens properly. It's also recommended to confirm the functionality with a simpler test scenario.
Zionnite
13 Jun, 2024, 08:36
TypeScript
    const result = await users.list(
      [Query.limit(500)]
    );
    
    const usersDocList = result.users;

  
    log(`userList ${usersDocList.length}`);
    for(let i=0; i<usersDocList.length; i++){
      let usersList     = usersDocList[i];
      let userId        = usersList.$id;
      let userPhone     = `+${usersList.$id}`;
      let userEmail     = usersList.email;
      let targets       = usersList.targets;

      
Zionnite
13 Jun, 2024, 08:37
TypeScript
      if(subscribeType == 'email'){
        log(`Email sub`);

        log(`UserId ${userId}`);
        log(`UserId ${userEmail}`);
        log(`targets ${targets.length}`);
        for(let j= 0; j<targets.length; j++){
          log(`email ${j}`);
          if(targets[j].providerType == 'email'){
            log(`started`); //================================================== IT GET STUCK HERE
            await messaging.createSubscriber(
              GENERAL_TOPIC_EMAIL, // topicId
              ID.unique(), // subscriberId
              targets[j].$id // targetId
            );
            log(`done`);
          }
        }
      }
 
Zionnite
13 Jun, 2024, 08:37
TypeScript

        for(let j= 0; j<targets.length; j++){
          if(targets[j].providerType == 'sms'){
            await messaging.createSubscriber(
              GENERAL_TOPIC_SMS, // topicId
              ID.unique(), // subscriberId
              targets[j].$id // targetId
            );
          }
        }
      }
      else if(subscribeType == 'push'){

        for(let j= 0; j<targets.length; j++){
          if(targets[j].providerType == 'push'){
            await messaging.createSubscriber(
              GENERAL_TOPIC_PUSH, // topicId
              ID.unique(), // subscriberId
              targets[j].$id // targetId
            );
          }
        }
      }
      else if(subscribeType == 'all'){

        for(let j= 0; j<targets.length; j++){


          if(targets[j].providerType == 'email'){
            await messaging.createSubscriber(
              GENERAL_TOPIC_ALL, // topicId
              ID.unique(), // subscriberId
              targets[j].$id // targetId
            );
          }

          if(targets[j].providerType == 'sms'){
            await messaging.createSubscriber(
              GENERAL_TOPIC_ALL, // topicId
              ID.unique(), // subscriberId
              targets[j].$id // targetId
            );
          }

          if(targets[j].providerType == 'push'){
            await messaging.createSubscriber(
              GENERAL_TOPIC_ALL, // topicId
              ID.unique(), // subscriberId
              targets[j].$id // targetId
            );
          }
        }
      }
    }

  
  }catch(appwriteErrors){

    error(appwriteErrors);
  }
  


  return res.send({"data": "end like this"});
  

};```
Zionnite
13 Jun, 2024, 08:38
Zionnite
13 Jun, 2024, 08:51

now, i wrap the subscribe endpoint into try and catch, here is what i'm getting now

TypeScript
        log(`Email sub`);

        log(`UserId ${userId}`);
        log(`UserId ${userEmail}`);
        log(`targets ${targets.length}`);
        for(let j= 0; j<targets.length; j++){
          log(`email ${j}`);
          if(targets[j].providerType == 'email'){
            log(`started`);
            try{

              await messaging.createSubscriber(
                GENERAL_TOPIC_EMAIL, // topicId
                ID.unique(), // subscriberId
                targets[j].$id // targetId
              );
            }catch(creatingError){
              error({
                "error":creatingError,
                "depth":'creatingErrorEmail'
              });
            }
            log(`done`);
          }
        }
      }```
Zionnite
13 Jun, 2024, 08:52
Zionnite
13 Jun, 2024, 08:59

Appwrite Cloud Function - createSubscriber EndPoint not working

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more