0

Trying to use Redis to pass updates to my HTML. However the Javascript is throwing an error that it cant find the url for the stream. I beleive the Redis is running but when i double click on the server file it immidiately closes however i do recieve a pong when i ping it.

var source = new EventSource("{{url_for('sse.stream')}}");
source.addEventListener('update', function(event){
    var data = JSON.parse(event.data);
    alert("The Server Says"+data.message);
}, false);
source.addEventListener('error',function(event){
    alert("Failed");
}, false);

app.config['REDIS_URL'] = "redis://localhost"
app.register_blueprint(sse, url_prefix='/stream')

@app.route('/UserUpdate')
@login_required
def user_update(stockvalue):
    print("There has been a sale")
    print(stockvalue)
    sse.publish({"CashUpdate":"£100"},type="update")

The Error is Failed to load resource: %7B%7Burl_for('sse.stream')%20%7D%7D:1 the server responded with a status of 404 (NOT FOUND)

  • 1
    Most likely there is a problem w/ putting Jinja into JS. Please see this SO answer for a workaround: https://stackoverflow.com/a/21628553/42346 – mechanical_meat Mar 19 '21 at 16:54
  • @mechanical_meat i followed what was commented however ive ended up with a path of "127.0.0.1/5000/undefined" i used this in my html and then tried to parse it in js var djangoData = $('#my-data').data(); var source = new EventSource(djangoData); – user11432150 Mar 19 '21 at 18:53
  • Oh, I just noticed that there might be a problem with how you register the blueprint. How is it defined in `sse.py`? It might need to be changed to: `app.register_blueprint(sse.bp)`. – mechanical_meat Mar 19 '21 at 21:36
  • @mechanical_meat i dont think ive understood the register blueprint. why cant the event messenger link to /UserUpdate – user11432150 Mar 20 '21 at 12:51

0 Answers0