O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Webchat using flask socket io

2.852 visualizações

Publicada em

Creating web chat server with Flask-SocketIO #pythonid

Publicada em: Internet
  • Seja o primeiro a comentar

Webchat using flask socket io

  1. 1. Creating Web Chat With Flask-SocketIO Linggar Primahastoko x@linggar.asia
  2. 2. Flask?
  3. 3. Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. http://flask.pocoo.org/
  4. 4. SocketIO Socket.IO enables real-time bidirectional event- based communication. It works on every platform, browser or device, focusing equally on reliability and speed. https://socket.io/
  5. 5. Why SocketIO?
  6. 6.  Bidirectional Communication  Async  Real Time  Fast
  7. 7. What can we do with SocketIO  Real Time Analytics  Binary Streaming (Since Socket.io v1.0)  Instant Messaging  Document Collaboration
  8. 8. Flask-SocketIO
  9. 9. Flask-SocketIO gives Flask applications access to low latency bi-directional communications between the clients and the server. The client- side application can use any of the SocketIO official clients libraries in Javascript, C++, Java and Swift, or any compatible client to establish a permanent connection to the server. https://flask-socketio.readthedocs.io
  10. 10. Why Flask-SocketIO  Simple Implementation  Rich of Features  Fast to be developed
  11. 11. Requirements
  12. 12. Server Side  Python 2.7 or Python 3.3+  Async packages − Eventlet − Gevent − Flask Development Server
  13. 13. Client Side  Official Socket.IO Javascript client library
  14. 14. Getting Started
  15. 15. Server Init from flask import Flask, render_template from flask_socketio import SocketIO app = Flask(__name__) app.config['SECRET_KEY'] = 'secret!' socketio = SocketIO(app) if __name__ == '__main__': socketio.run(app)
  16. 16. Client init <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1. 3.6/socket.io.min.js"></script> <script type="text/javascript" charset="utf-8"> var socket = io.connect('http://' + document.domain + ':' + location.port); socket.on('connect', function() { socket.emit('my event', {data: 'I'm connected!'}); });
  17. 17. On Connect https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/example/app.py
  18. 18. Background task https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/example/app.py
  19. 19. Sending Message  Send − implemented for compatibility with vanilla WebSocket interface − Only send string  Emit − feature of Socket.IO − Can send object
  20. 20. https://flask-socketio.readthedocs.io/en/latest/
  21. 21. Namespace Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths. This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels. https://socket.io/docs/rooms-and-namespaces
  22. 22. Receiving message index.html index.html app.py
  23. 23. Broadcast Send “A” Send “A” Send “A” Client A Client B Client C Server
  24. 24. Session Every connected client has session id from flask import request session_id = request.sid
  25. 25. Room Flask-SocketIO Built-in Function:  rooms  join_room  leave_room  close_room
  26. 26. Room Room A Room B Server Client Client Client ClientClient Client Client Client Client Client # Join Room join_room(room_name) # Send message to room emit('room_response', {'message': 'test'}, room=room_name)
  27. 27. Private Message Client Client Client Server Send 'A' Send 'A' # Send private message emit('my_response', {'message': 'message'}, room=friend_session_id)
  28. 28. What's next?
  29. 29. Flask-IRC  Server Side  Flask  Flask-SocketIO  Client Side  SocketIO Javascript Client Library  jQuery
  30. 30. https://github.com/linxlunx/flask-irc
  31. 31. Thank You!

×