Firebase AfterSave function like Parse.com

前端 未结 2 429
执笔经年
执笔经年 2020-12-11 10:56

So I was using Parse.com AfterSave cloud code function to keep a list of the most recent Posts relating to a specific location. The reason for this is to get the most recent

相关标签:
2条回答
  • 2020-12-11 11:59

    A couple of thoughts; one of which you already mentioned as 'no' but wanted to include it for completeness:

    all_messages
      message_0
        message: blah blah
        timestamp: the timestamp
      message_1
        message: blah blah
        timestamp: the timestamp
      message_2
        message: blah blah
        timestamp: the timestamp
    

    If all clients are observing the all_messages node, any time a new message is added, the clients will be notified and can query for the last three messages by timestamp. Pretty simple solution.

    Another thought is to have a node that keeps a reference to just the last three messages, reducing overhead and sorting.

    all_messages
      message_45
        message: blah blah
      message_50
        message: blah blah
      message_51
        message: blah blah
    
    last_three_messages
      last_message_0: message_45
      last_message_1: message_50
      last_message_2: message_51
    

    When a client writes a message to the all_messages node, write a reference to it in the last_three_messages node and shuffle the older messages down.

    With this, clients can just observe the last_three_messages node.

    It will take a little client logic to handle pushing the most current message onto the last_message_0 slot and then shuffling the other down but it would only take a few lines of code with minimal overhead.

    0 讨论(0)
  • 2020-12-11 12:00

    There is currently no way to run your code on Firebase's servers. So you will have to come up with a less-direct mapping of Cloud Code to a new solution. Running the functionality on each client is one option, running it on a cheap "bot"/server is another one.

    I'll find a few questions where this has been covered before and link them here:

    • Firebase and backend logic (recent question that covers using a nodejs server)
    • How would I run server-side code in Firebase?
    • How do I use Firebase to handle automatic server-side calculations?
    • Firebase and indexing/search
    • Server Side Calculation using Firebase (which leads to four more questions about this)

    One thing to keep in mind is that running a server like this is not the type of "writing a server" that you're immediately thinking of. Most of these act more like a "bot" that interacts with Firebase in the same way as your client-side code does. The only difference in that case is that it runs on an environment you control, so that it can run with elevated credentials.

    0 讨论(0)
提交回复
热议问题