Skip to content
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

Merged
merged 3 commits into from
Nov 27, 2018
Merged

Add a chat demo using Flask (Python) #32

merged 3 commits into from
Nov 27, 2018

Conversation

dunglas
Copy link
Owner

@dunglas dunglas commented Nov 26, 2018

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

mercure-chat-python

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.

e.preventDefault()

const body = new URLSearchParams({
data: JSON.stringify({ username: this.elements.username.value, message: this.elements.message.value }),

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 ?

Copy link
Owner Author

@dunglas dunglas Nov 27, 2018

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.

@dunglas dunglas merged commit b3dd0b3 into master Nov 27, 2018
)

hub_url = os.environ.get('HUB_URL', 'http://localhost:3000/hub')
topic = os.environ.get('HUB_URL', 'http://example.com/chat')
Copy link
Contributor

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.

@dunglas dunglas deleted the chat branch January 9, 2019 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants