0

I am building a small social media site, and I am trying to make a system, where if you like a post, the user who owns the post gets a notification. This is the function that gets executed when the post is liked:

def notification():
   print("STARTING")
   socketio.emit('notification', {'data': 'got it!'})
   print("DONE")

Now, when a post is liked everyone gets an alert in javascript. How could I make it so that only the user who owns the post gets this notification, and only if the user is currently on the site?? Thank you in advanced!

HCook886
  • 63
  • 6
  • Might [this post](https://stackoverflow.com/q/39423646/15368978) answer your question? – Patrick Yoder May 09 '22 at 14:47
  • 1
    Hi! Thank you for your answer! This could be done, but I wanted to know if there was a better to do this. Creating a room for each user seems quite inefficient. Plus, I want to send it to a user determined by their user id. – HCook886 May 09 '22 at 18:34
  • Actually, [this post](https://stackoverflow.com/q/66610562/15368978) might be more relevant. – Patrick Yoder May 09 '22 at 19:26
  • 1
    I read rhis article, [link](https://stackoverflow.com/questions/66610562/socketio-to-emit-to-a-particular-user-without-using-separate-room-for-every-cli) and it seems almost right. The only problem is that I need the sid of another user, not the one sending the request. How can I achieve this? Thank you! – HCook886 May 10 '22 at 06:37
  • Would it work to create a dictionary with user ids as keys and their corresponding `sid` as values? You would then update this dictionary in your socket `connect` function? I don't know, these are just my thoughts, I should probably let someone more experienced help you. – Patrick Yoder May 10 '22 at 14:34
  • 1
    Hi! This is exactly what I am trying to do now. This seems like a good method for now, but not very scalable. I would guess it would be complicated to do this with 10.000 users. Thank you for your help! – HCook886 May 11 '22 at 07:24
  • I have idea but not tested it, you may store in session unique secret id for the logged user and pass this id to the client side , also instead of naming the event notification, name it `notification + uniqueid`, so only the target user will know the id to access this event – Mahmoud Magdy Sep 13 '22 at 16:16

0 Answers0