Back

Appwrite send message to client?

  • 0
  • Functions
Sky
17 Mar, 2023, 15:40

Hey all, in my project I needed to have different session limits per user. So with the advice of Matej, I created a collection that stores attributes userId and limit.

Then I created a function that fired on users..sessions..create

It checks if a user's session has gone over their limit. Right now if that is true, it will delete the session. The issue is I need to send the response to the client with the error message, with the idea that I can deny the login request.

I was looking through the documentation, I'm not sure how to send a message back to the client manually. I think as an API this the things like res.json({}) are supposed to be returned?

I think a workaround, if I can send any type of message to the client I could have them listen and then fire the logout() that way.

The client is using appwrite web sdk and svelte

TL;DR
The user is trying to send a message to the client using Appwrite. They are looking for a way to send a response to the client with an error message in order to deny a login request. They are unsure of how to do this and are considering using the `logout()` function as a workaround. The user is using the Appwrite web SDK and Svelte. Solution: One possible solution is to use the Appwrite Realtime feature to subscribe to updates and fire an event when the session is deleted. Additionally, the user can try using the `res.json` function to send a response to the client with an error message. The
Sky
17 Mar, 2023, 15:55

Here is my function:

TypeScript

module.exports = async function (req, res) {
  if (req.headers != 'website') {
    const client = new sdk.Client();
    const database = new sdk.Databases(client);
    
    client
    .setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
    .setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
    .setKey(req.variables['APPWRITE_FUNCTION_API_KEY']);

    const users = new sdk.Users(client);
    const docs = database.listDocuments('640c0c2b8e955c6e66ae', '640c0c315d02c25dc798'); // database ID, collection ID
    let data = null;
    let userId = null;

    try {
      data = JSON.parse(req.variables.APPWRITE_FUNCTION_EVENT_DATA);
      userId = data.userId;
    } catch (err) {
      res.json({'error': err.toString()});
    }
    
    docs.then(function (documents) {
      // let id = result.documents[0].userId
      // let limit = result.documents[0].limit
      let doc = null;

      for (let i = 0; i < documents.documents.length; i++) {
        if (documents.documents[i].userId == userId) {
          doc = documents.documents[i];
          break;
        }
      }

      if (doc) {
        const sessions = users.listSessions(userId);
        sessions.then(function (userSessions) {
          // res.json({documents, userSessions}); // Success
          if (userSessions.total > 1) {
            if (userSessions.total > doc.limit) {
              res.send("user sessions over document limit");
              // delete session
            }
            
          } else {
            res.send("1 session");
          }
        }, function (err) {
          res.json({'error': err.toString()})
        }); 
      }

    }, function (error) {
        res.json({'error': error.toString()})
    });

  }
};```
rafagazani
17 Mar, 2023, 16:02

I would try with realtime and listen to the account event

Binyamin
17 Mar, 2023, 16:04

There is no way to send information out But, you do have few options to tackle this challenge you can try to use AppWrite realtime like so (in case your function id is aaa111bbb.

TypeScript
client.subscribe('functions.aaa111bbb.executions.*.update', response => {
  // check the user login status for the session
});

But it's sound like to much resources for this task

Sky
17 Mar, 2023, 16:06

oh like I would do that client-side?

Binyamin
17 Mar, 2023, 16:06

This is one way, yes

Binyamin
17 Mar, 2023, 16:07

Or you can give the user just reading access for the document length and listen to it in realtime So in that case the only the realtime will fire a new event is when is session got deleted

Sky
17 Mar, 2023, 16:08

oh okay that is awesome, theoretically I could subscribe to the account or session client side? and if that changes, i can logout

Binyamin
17 Mar, 2023, 16:08

Exactly

Sky
17 Mar, 2023, 16:09

thanks so much guys 🙂

Sky
17 Mar, 2023, 16:09

appwrite is amazing

Binyamin
17 Mar, 2023, 16:09

Indeed

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