Good day.
There was an old idea to make your chat, with its public API, and optimize everything as much as possible.
I tried to work with Aiohttp and AsyncIO nothing good AsyncIO to me. Returned to the origins, namely Flask
The chat worked as it should any chat: it transmitted messages that were entered into the event database on Redis and the second stream warned the server every time during the new event and the server responded to the socket clients on the chat itself.
Everything is perfect, but I did not use the WebSocket protocol and the entire Socket Server worked in threading async mode . I noticed that after 5-6 connected clients, the server simply stopped responding and kept clients on the connection in perpetual waiting mode
I decided to try again to look at the docks, found other modes of operation, among which were eventlet and gevent
Problems encountered:
eventletThe client connected viaWebsocketcould make requests to the server, but he did not receive anything back.geventIt seems like it works and request and respons onWebSocketbut the answer had to wait about 10-20 seconds.
UPDATE: If you send another message to the queue while gevent, then the past will immediately come ¯_ () _ / ¯
JS client:
<script> var namespace = '' var socket = io.connect('http://weetway.ru:5000' + namespace, {transports: ['websocket']}) socket.on( 'connect', function() { //socket.emit('getTs', {'ts': '0'}) var form = $( 'form' ).on( 'submit', function( e ) { e.preventDefault() let user_name = encodeURI($( 'input.username' ).val()) let user_input = encodeURI($( 'input.message' ).val()) console.log(user_name) console.log(user_input) socket.emit( 'sendMessage', { 'name' : user_name, 'message' : user_input }) // empty the input field $( 'input.message' ).val( '' ).focus() } ) } ) // capture message socket.on('getTs'), function (ts) { ts = ts } socket.on('polling', function( msg ) { console.log('New Event -> ' + msg) //msg = JSON.parse(msg) if (typeof msg.username !== 'undefined') { //console.log('Gotcha!') $('h1').remove() $('div.message_holder').append('<div id="ts-' + msg.timestamp + '" class="msg_bbl"><b style="color: #000">' + msg.username + ':</b> ' + msg.message + '</div>') $('#ts-' + msg.timestamp).ScrollTo(); } }) </script> Python server:
@pollingup._polling.on('polling') def view(event): event = str(event) event = urllib.parse.unquote(event) print(newEvent(str(event))) socketio.emit('polling', eval(event)) @socketio.on('sendMessage') def newMessage(json): chat.sendMessage(name=json['name'], message=json['message']) If necessary, you can go to view with the open console F12 - http : //weetway.ru.l000
Thanks in advance for your help
WebSockethas beenWebSocketfor a long time. - Andsocket.iowithsocket.iotransport can use? - D-sideisocket, there is no point in using theisocketlibrary, because there is already built-in support in the browser, and on the server, everything is done as standard. - Andsocket.ioit is called. It would have happened ifsocket.iowas almost a bare wrapper over WS, but it has enough of its own chips. - D-side