Skip to content

Instantly share code, notes, and snippets.

View dandynaufaldi's full-sized avatar
:octocat:
working

Dandy Naufaldi dandynaufaldi

:octocat:
working
View GitHub Profile
@dandynaufaldi
dandynaufaldi / app.py
Created October 25, 2020 20:42
Flask with SSE
import time
from flask import Flask, request
from flask_sse import sse
app = Flask(__name__)
app.config["REDIS_URL"] = "redis://localhost:6379"
app.register_blueprint(sse, url_prefix="/stream")
@app.route("/chat", methods=["POST"])
@dandynaufaldi
dandynaufaldi / stream.js
Created October 25, 2020 20:45
Consume SSE in client
function appendChat(chat) { ... }
function sendChat() {
event.preventDefault();
const chatForm = document.getElementById("chat-form");
const chatMessage = chatForm.elements.message.value;
const xhr = new XMLHttpRequest();
xhr.open('POST', 'chat');