mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-24 16:24:08 -04:00
install socket io and getting an example up
This commit is contained in:
@@ -20,6 +20,7 @@ from flask import (
|
||||
)
|
||||
from flask.globals import request_ctx
|
||||
from flask_login import LoginManager, current_user
|
||||
from flask_socketio import SocketIO
|
||||
from flask_talisman import Talisman
|
||||
from flask_wtf import CSRFProtect
|
||||
from flask_wtf.csrf import CSRFError
|
||||
@@ -121,6 +122,7 @@ from notifications_utils.url_safe_token import generate_token
|
||||
login_manager = LoginManager()
|
||||
csrf = CSRFProtect()
|
||||
talisman = Talisman()
|
||||
socketio = SocketIO()
|
||||
|
||||
# The current service attached to the request stack.
|
||||
current_service = LocalProxy(partial(getattr, request_ctx, "service"))
|
||||
@@ -217,6 +219,7 @@ def create_app(application):
|
||||
|
||||
init_govuk_frontend(application)
|
||||
init_jinja(application)
|
||||
socketio.init_app(application, cors_allowed_origins=['http://localhost:6012'])
|
||||
|
||||
for client in (
|
||||
csrf,
|
||||
|
||||
20
app/assets/javascripts/socketio.js
Normal file
20
app/assets/javascripts/socketio.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const socket = io();
|
||||
const jobId = document.querySelector("[data-job-id]").dataset.jobId;
|
||||
|
||||
socket.on('connect', () => {
|
||||
console.log('Connected to server');
|
||||
});
|
||||
|
||||
|
||||
// Join a room
|
||||
socket.emit("join", { room: `job-${jobId}` });
|
||||
console.log("🧠 Joined room:", `job-${jobId}`);
|
||||
|
||||
// Listen for job updates
|
||||
socket.on("job_update", function (data) {
|
||||
const el = document.querySelector(`[data-key="${data.key}"]`);
|
||||
if (el) {
|
||||
el.textContent = "✅ Real-time update success!";
|
||||
el.classList.add("text-success"); // Add a class instead
|
||||
}
|
||||
});
|
||||
@@ -21,6 +21,7 @@ from app import (
|
||||
format_datetime_table,
|
||||
notification_api_client,
|
||||
service_api_client,
|
||||
socketio,
|
||||
)
|
||||
from app.formatters import get_time_left, message_count_noun
|
||||
from app.main import main
|
||||
@@ -28,6 +29,7 @@ from app.main.forms import SearchNotificationsForm
|
||||
from app.models.job import Job
|
||||
from app.utils import parse_filter_args, set_status_filters
|
||||
from app.utils.csv import generate_notifications_csv
|
||||
from flask_socketio import emit, SocketIO, send, join_room
|
||||
from app.utils.pagination import (
|
||||
generate_next_dict,
|
||||
generate_previous_dict,
|
||||
@@ -57,6 +59,7 @@ def view_job(service_id, job_id):
|
||||
|
||||
filter_args = parse_filter_args(request.args)
|
||||
filter_args["status"] = set_status_filters(filter_args)
|
||||
|
||||
return render_template(
|
||||
"views/jobs/job.html",
|
||||
job=job,
|
||||
@@ -103,9 +106,11 @@ def view_job_csv(service_id, job_id):
|
||||
@user_has_permissions("send_messages")
|
||||
def cancel_job(service_id, job_id):
|
||||
Job.from_id(job_id, service_id=service_id).cancel()
|
||||
|
||||
return redirect(url_for("main.service_dashboard", service_id=service_id))
|
||||
|
||||
|
||||
# //this is the resource that gets updated by being passed to ajax via updates_url to data-resource
|
||||
# and everything that is within get_job_partials is the html that gets updated
|
||||
@main.route("/services/<uuid:service_id>/jobs/<uuid:job_id>.json")
|
||||
@user_has_permissions()
|
||||
def view_job_updates(service_id, job_id):
|
||||
@@ -113,6 +118,38 @@ def view_job_updates(service_id, job_id):
|
||||
|
||||
return jsonify(**get_job_partials(job))
|
||||
|
||||
# def emit_job_update(service_id, job, job_id):
|
||||
# print("⚡️ Emitting job update for:", job_id)
|
||||
# socketio.emit(
|
||||
# "job_update",
|
||||
# {"key": "status", "html": render_template("partials/jobs/status.html", job=job)},
|
||||
# room=f"job-{job_id}"
|
||||
# )
|
||||
|
||||
@socketio.on("join")
|
||||
def on_join(data):
|
||||
room = data["room"]
|
||||
join_room(room)
|
||||
print(f"Client joined room {room}")
|
||||
|
||||
@main.route("/services/<uuid:service_id>/jobs/<uuid:job_id>/test-socket-update")
|
||||
def test_socket_update(service_id,job_id):
|
||||
print(f"⚡️ Emitting job update for: {job_id}")
|
||||
# Replace with a real job ID and service ID for testing
|
||||
job = Job.from_id(job_id, service_id)
|
||||
service_id=service_id
|
||||
job_id = job_id
|
||||
|
||||
# Call your emit function
|
||||
socketio.emit(
|
||||
"job_update",
|
||||
{
|
||||
"key": "status",
|
||||
"html": "<div class='text-success'>✅ Real-time update success!</div>"
|
||||
},
|
||||
room=f"job-{job_id}" # match the room name used in frontend
|
||||
)
|
||||
return "Update sent!"
|
||||
|
||||
@main.route("/services/<uuid:service_id>/notifications", methods=["GET", "POST"])
|
||||
@main.route(
|
||||
|
||||
@@ -4,7 +4,7 @@ from zoneinfo import ZoneInfo
|
||||
from app.extensions import redis_client
|
||||
from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
|
||||
from app.utils.csv import get_user_preferred_timezone
|
||||
|
||||
# from app.main.views.jobs import emit_job_update
|
||||
|
||||
class JobApiClient(NotifyAdminAPIClient):
|
||||
JOB_STATUSES = {
|
||||
|
||||
@@ -12,3 +12,17 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro socket_block(partials, job_id, key, finished=False, form='') %}
|
||||
{% if not finished %}
|
||||
<div
|
||||
data-module="live-content"
|
||||
data-key="{{ key }}"
|
||||
data-room="job-{{ job_id }}"
|
||||
>
|
||||
{% endif %}
|
||||
{{ partials[key]|safe }}
|
||||
{% if not finished %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
@@ -3,12 +3,21 @@
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
{% from "components/page-header.html" import page_header %}
|
||||
{% from "components/components/back-link/macro.njk" import usaBackLink %}
|
||||
|
||||
{% from 'components/ajax-block.html' import socket_block %}
|
||||
{% block service_page_title %}
|
||||
{{ "Message status" }}
|
||||
{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
{{ socket_block(partials, job_id, "status") }}
|
||||
<div id="status" data-key="status" data-job-id="{{ job.id }}">
|
||||
⏳ Waiting for updates...
|
||||
</div>
|
||||
<!-- <div data-key="status">
|
||||
{{ partials["status"]|safe }}
|
||||
</div> -->
|
||||
<!-- {{emit_job_update}}
|
||||
{{job.id}} -->
|
||||
|
||||
{{ page_header("Message status") }}
|
||||
{% if not job.finished_processing %}
|
||||
|
||||
Reference in New Issue
Block a user