Added server side events (SSE) to update client datatables.

This commit is contained in:
Louis Vézina 2019-12-17 23:37:38 -05:00
parent 6f378ad2e3
commit 43b9a8d0fe
3 changed files with 54 additions and 2 deletions

33
bazarr/SSE.py Normal file
View File

@ -0,0 +1,33 @@
from __future__ import absolute_import
from collections import deque
import json
import time
class EventStream:
"""
This class is used to read or write items to the notifications deque.
"""
def __init__(self):
self.queue = deque(maxlen=10)
def write(self, msg):
"""
:param msg: The message to display.
:type msg: str
"""
self.queue.append("data:" + msg + "\n\n")
def read(self):
"""
:return: Return the oldest notification available.
:rtype: str
"""
if self.queue or (len(self.queue) > 0):
return self.queue.popleft()
event_stream = EventStream()

View File

@ -14,11 +14,14 @@ from database import database
from helper import path_replace, path_replace_reverse, path_replace_movie, path_replace_reverse_movie from helper import path_replace, path_replace_reverse, path_replace_movie, path_replace_reverse_movie
from get_languages import load_language_in_db, alpha2_from_language, alpha3_from_language, language_from_alpha2, \ from get_languages import load_language_in_db, alpha2_from_language, alpha3_from_language, language_from_alpha2, \
alpha3_from_alpha2 alpha3_from_alpha2
from SSE import event_stream
from flask import Flask, jsonify, request, render_template from flask import Flask, jsonify, request, render_template
import flask
from flask_restful import Resource, Api from flask_restful import Resource, Api
app = Flask(__name__) app = Flask(__name__)
app.debug = True
api = Api(app) api = Api(app)
load_language_in_db() load_language_in_db()
@ -28,6 +31,17 @@ def test():
return render_template('test.html') return render_template('test.html')
@app.route('/event')
def event():
return flask.Response(event_stream.read(), mimetype="text/event-stream")
@app.route('/write')
def write():
event_stream.write('fake message')
return "", 200
class Badges(Resource): class Badges(Resource):
def get(self): def get(self):
result = { result = {
@ -438,4 +452,4 @@ api.add_resource(WantedSeries, '/api/wanted_series')
api.add_resource(WantedMovies, '/api/wanted_movies') api.add_resource(WantedMovies, '/api/wanted_movies')
if __name__ == '__main__': if __name__ == '__main__':
app.run(port=6767, debug=True) app.run(port=6767, threaded=True)

View File

@ -23,7 +23,7 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
$('#example').DataTable( { var table = $('#example').DataTable( {
"processing": true, "processing": true,
"serverSide": true, "serverSide": true,
"ajax": "http://localhost:6767/api/history_movies", "ajax": "http://localhost:6767/api/history_movies",
@ -34,6 +34,11 @@
{ "data": "description" } { "data": "description" }
] ]
} ); } );
var source = new EventSource('/event');
source.onmessage = function (event) {
table.ajax.reload(null, false);
};
} ); } );
</script> </script>
</html> </html>