[PYTHON] [Chat De Tornado] Make a chat using WebSocket with Tornado

Introduction

This time, I will create a chat using Tornado, a web server and web framework made by Python. The reason for using Tornado is that it supports WebSocket by default, so it is easy to create real-time services.

What to use

The following two are used.

Use Tornado on the server side and jquery.ui.chatbox on the client side. Chatting is relatively easy with jquery.ui.chatbox.

code

The following code is the main processing on the server side. It records the messages sent with the people connecting to the waiters and messages.

class ChatHandler(tornado.websocket.WebSocketHandler):

    waiters = set()
    logs = []

    def open(self, *args, **kwargs):
        self.waiters.add(self)
        self.write_message({'logs': self.logs})

    def on_message(self, message):
        message = json.loads(message)
        self.logs.append(message)
        for waiter in self.waiters:
            if waiter == self:
                continue
            waiter.write_message({'img_path': message['img_path'], 'message': message['message']})

    def on_close(self):
        self.waiters.remove(self)

The ** open ** method registers the person who has connected and sends the log so far to that person.

The ** on_message ** method broadcasts the message sent when the message was sent to participants other than yourself. Also, add the message sent at this time to the log.

The ** on_close ** method removes the connecter from the waiters when the connection is lost. This prevents the message from being broadcast to the disconnected person.

Finished product

The following is the finished product. I made it in about an hour, but it works fine. The feature is that you can open and close the chat screen by clicking the bar at the top. スクリーンショット 2015-12-14 0.17.43.png

Sample code

Click the link below for sample code.

in conclusion

This time I made a chat app using Tornado. If you want to create a little real-time application in Python, you should use Tornado.

Recommended Posts

[Chat De Tornado] Make a chat using WebSocket with Tornado
Let's make a web chat using WebSocket with AWS serverless (Python)!
Make a LINE BOT (chat)
Using a printer with Debian 10
Make a fortune with Python
Make a fire with kdeplot
Let's make a websocket client with Python. (Access token authentication)
Let's make a GUI with python.
Make a face recognizer using TensorFlow
Make a recommender system with python
Make a filter with a django template
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a model iterator with PySide
Make a nice graph with plotly
Using a webcam with Raspberry Pi
I tried to make a todo application using bottle with python
I made a poker game server chat-holdem using websocket with python
Let's make a shiritori game with Python
Make a video player with PySimpleGUI + OpenCV
Make a rare gacha simulator with Flask
Make a Notebook Pipeline with Kedro + Papermill
Make a partially zoomed figure with matplotlib
Make a cascade classifier with google colaboratory
Let's make a simple language with PLY 1
Make a logic circuit with a perceptron (multilayer perceptron)
Make a Yes No Popup with Kivy
Let's make a multilingual site using flask-babel
Make a wash-drying timer with a Raspberry Pi
Make a GIF animation with folder monitoring
Equipped with a card function using payjp
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
A memorandum to make WebDAV only with nginx
Investment quest: Make a system trade with pyhton (2)
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Make a monitoring device with an infrared sensor
Make a simple pixel art generator with Flask
How to make a dictionary with a hierarchical structure.
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Make holiday data into a data frame with pandas
Make a LINE WORKS bot with Amazon Lex
Let's make a module for Python using SWIG
Create a "Hello World" (HTTP) server with Tornado
I tried using a database (sqlite3) with kivy
(Memorandum) Make a 3D scatter plot with matplodlib
I tried to make a ○ ✕ game using TensorFlow
Make one repeating string with a Python regular expression.
A story about using Resona's software token with 1Password
Make a morphological analysis bot loosely with LINE + Flask
Try to make a command standby tool with python
[Practice] Make a Watson app with Python! # 2 [Translation function]