-
-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a chat demo using Flask (Python) #32
Conversation
e.preventDefault() | ||
|
||
const body = new URLSearchParams({ | ||
data: JSON.stringify({ username: this.elements.username.value, message: this.elements.message.value }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to send the username if the user is logged ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is authorized to subscribe and to post to the hub, not really "logged in" (I mean it is authorized but not authenticated).
A proper login system could be added to the Python app (if appropriate), then the username could be set in the dedicated JWT claim, and sent along with the message. But then I think that it would be more efficient (and more secure) to send new messages to the Python app, that will then send them (including the current username) to the hub.
) | ||
|
||
hub_url = os.environ.get('HUB_URL', 'http://localhost:3000/hub') | ||
topic = os.environ.get('HUB_URL', 'http://example.com/chat') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this env variable has to be TOPIC, not HUB_ URL.
This PR adds an example of a real-time chat app in 23 lines of Python (2 dependencies: the Flask microframework and PyJWT) and 35 lines of JavaScript (0 dependencies). Of course it benefits from built-in capabilities of the Mercure protocol: HTTP/2/HTTP/3 compatibility, automatic reconnection with re-fetching of missed messages, authentication...
live demo
Flask is only used to handle the authentication part (cookie method) and to serve the HTML layout. The chat system itself uses directly the Mercure hub.
馃樆
Closes #12.