-2

I built a small notification api system that has id, user_id, subject, text, status, created_at columns in the data base.

My current process is that when a user requests for her notifications i get the most recent 100 unread messages and send to the user, then wait for the front end to send me a list of ids of those that have been read by the user so that i can change their status to read.

but sometimes this does not happen meaning the when the user requests for another set of messages they will get back their already read messages.

So i am thinking of marking the messages as read once i retrieve them for the user rather than wait for update from the frontend.

but i am not sure if this is the best way to handle this! is there a better process for api notification systems please advice me. Thanks.

3
  • even if my question is so unimportant and you know the solution please tell along with your down votes
    – Sayra
    May 28, 2020 at 21:17
  • This might be a race condition in your code. If the client does not send the list of IDs back to the server then on his next query she will receive the same messages.
    – px1mp
    May 28, 2020 at 21:19
  • @px1mp yes that is what i currently happening but the read messages on the frontend now show up as unread once again
    – Sayra
    May 28, 2020 at 21:22

2 Answers 2

1

I would think about it like this: What is that listing of unread items depicting? What the state understands to have been left unread. Is pulling a listing of unread items really meant to denote all those items were read? I think not. You may allow a bulk mark read, but reading the list is just a stateful representation, of fact. Read the list, and authoritatively mark read when touched by a visible interface component by sending a per-item request to thing/mark-read.

If you're having issues with the (central) store not representing state accurately because you think they should be read, debug your interface. I would not mark read on pulling the list, though. That would be a flawed approach.

If you do directly insert all 100 into the display where you consider them read, create an endpoint in your api for thing/mark-items-read and pass it those 100 item ID's.

1

We can't see your code here, but I would suggest the following:

  1. Store the ids of messages displayed to the use in an array on the frontend, so when a user views a message, you add the id to that array. I'm not sure what your frontend is using so can't really make a code suggestions.

  2. Create an endpoint that receives this array on the backend in your routes file

    Route::post('somepath/read-messages', 'MessageController@readMessages');
    

    Then, in your message controller:

    public function readMessages(Request $request) {
    
       if ($request->has('read_messages') {
           $messages = Message::whereIn('id', $request->read_messages)->get();
           $messages->update(array('staus' => 'read');
       }
    
    }
    
3
  • Thank you @Cornel Verster i guess it cause of the lack of code that i was down voted but my issue was not a code problem but a how to do it problem. i wanted to know if its better to wait and receive updates about the read messages or mark them as read after i have sent it out to the client? thats all. Also the frontend is react native thanks for your answer anyway, much appreciated.
    – Sayra
    May 28, 2020 at 22:21
  • Some advice: in general its better to ask code-related questions on stack overflow more than open-ended questions like "what is a better way to..." But I've made that mistake many times. Rather try Google those and see this May 28, 2020 at 22:24
  • Thanks i will keep that in mind but what of question like mine where can i get answers or suggestion its not like there is a book on notifications?
    – Sayra
    May 28, 2020 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.