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

Conversation

Projects
None yet
5 participants
@dunglas
Owner

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.

@dunglas dunglas force-pushed the chat branch from ce7033b to c892f6f Nov 26, 2018

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

This comment has been minimized.

@tlenclos

tlenclos Nov 27, 2018

Why do you need to send the username if the user is logged ?

This comment has been minimized.

@dunglas

dunglas Nov 27, 2018

Owner

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

1 check passed

coverage/coveralls Coverage remained the same at 93.164%
Details
)
hub_url = os.environ.get('HUB_URL', 'http://localhost:3000/hub')
topic = os.environ.get('HUB_URL', 'http://example.com/chat')

This comment has been minimized.

@aslafy-z

aslafy-z Nov 29, 2018

Contributor

I think this env variable has to be TOPIC, not HUB_ URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment