'How to send user specific data using SSE

i am trying to create a social media page where in home page of every user they can see feeds,

feeds from friend's post. when ever my friend create a post i can see the same in my feeds in real time.

For that i am using SSE in python flask. everything working find but after adding few more users only i realise all the post are coming to all logged in people's feed. which wrong, i want to see feeds from only my friends.

Can any one help me how to achieve it. i am sharing the base level code of python and java script.

Client side:

var source = new EventSource("http://172.19.0.3:8044/events");

source.addEventListener('user_feeds', function(event) {
    var data = JSON.parse(event.data);
    console.log("Even from server ");
    console.log(data);
}, false);

Server side

from flask_cors import CORS
from flask_sse import sse
from factory import create_app
app = create_app()
CORS(app)
app.config["REDIS_URL"] = "redis://redis"
input_user_feeds = dict()
app.register_blueprint(sse, url_prefix='/events)
PROMOTION_BLUEPRINT = Blueprint('my_page', __name__, url_prefix='/api/v1/')


@PROMOTION_BLUEPRINT.route('/feeds/<user_id>', methods=["GET"])
def feeds(user_id):
    push_feeds(user_id)
    return "SUCCESS"


@PROMOTION_BLUEPRINT.route('/user_request/<user_id>', methods=["POST"])
def user_request(user_id):
    data = request.json
    add_feeds(user_id, data)
    return "SUCESS"


def push_feeds(user_id):
    while 1 == 1:
        if user_id in input_user_feeds:
            input_request = input_user_feeds[user_id]
            sse.publish(input_request, type='user_feeds')
            del input_user_feeds[user_id]


def add_feeds(user_id, data):
    input_user_feeds[user_id] = data


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=Config.PORT, debug=True)

I trued to do with single user id. but that is also not a good idea. It will be helpful if anyone having good knowledge in SSE help me find the solution.

Thanks in advance.




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source