Support for web socket server

368 views
Skip to first unread message

nk sivaraju

unread,
Aug 23, 2021, 2:09:34 AM8/23/21
to py4web
Hi, I need to push the data to the UI whenever something in the db changes. As this can be achieved  with web socket server, would like to know does py4web support web socket protocol? if yes is there any simple way to integrate ws server to the existing app? links to any examples would be more useful. Thanks in advance.

Alexander Beskopilny

unread,
Aug 23, 2021, 3:42:15 AM8/23/21
to py4web
ex1 (with internal py4web-tornado-server)
https://github.com/ali96343/py4web-chat

ex2 (with intermediate uvicorn-channel-server)
https://github.com/ali96343/lvsio

nk sivaraju

unread,
Aug 23, 2021, 4:15:29 AM8/23/21
to py4web
I want to achieve auto refresh in the UI dashboard. I wonder if there is any direct way of achieving it using Py4web . 

Alexander Beskopilny

unread,
Aug 23, 2021, 4:22:17 AM8/23/21
to py4web
ex3  (push data to user and update UI with celery and intermediate tornado-server)
 https://github.com/ali96343/capp

( it seems to me that the best solution is to use nodejs as an intermediate socketio channel-server )

António Ramos

unread,
Aug 25, 2021, 5:37:52 AM8/25/21
to Alexander Beskopilny, py4web
why not a cloud service 

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/350d9b17-16c9-47b4-a5b4-4548d50230ban%40googlegroups.com.

Alexander Beskopilny

unread,
Aug 26, 2021, 2:04:51 AM8/26/21
to py4web
How to push event to js-UI from py4web controllers.py
1 in controllers.py
https://github.com/ali96343/lvsio/blob/main/lvsio/controllers.py
----------------------------------------------------------
......
import socketio
@action("sio_pusher", method=["GET", "POST"])
def sio_pusher():
    import datetime
    data_str = datetime.datetime.now().strftime("%d.%m.%y %H:%M:%S")
    r_url = 'redis://'
    mgr = socketio.RedisManager(r_url, write_only=True)
    mgr.emit('frompydal',  data_str, broadcast=True, include_self=False )
    return f'sio_pusher {data_str}'
   
   # pip install httpie 
   #  http localhost:8000/lvsio/sio_pusher

.......
------------------------------------------------------------------
2  https://github.com/ali96343/lvsio/blob/main/lvsio/chan_3000.py
# ----------------------------------------------------------------
# pip install aioredis==1.3.1
r_url = "redis://"
mgr = socketio.AsyncRedisManager(r_url, write_only=False)
sio = socketio.AsyncServer(
    async_mode="asgi",
    client_manager=mgr,
    cors_allowed_origins="*",
    SameSite=None,
    logger=True,
    engineio_logger=True,
)
app = socketio.ASGIApp(sio, static_files={"/": "./public/index.html"})

# --------------------------- utils ----------------------------------------------

..................................
socket.on('connect', function() {
                socket.emit('sync_hello_connect', {data: 'from browser: connected!'});
                // console.log('sync connected')
            });

            socket.on('frompydal', function(msg) {
                console.log('frompydal !!!!!!!!!!!!!!!!!!!!!');
                console.log(msg);
            });
..................
--------------------------------
thats it

Alexander Beskopilny

unread,
Aug 26, 2021, 4:23:27 AM8/26/21
to py4web
how write  intermediate-channel-server 

Miguel Grinberg

nk sivaraju

unread,
Sep 15, 2021, 1:10:09 AM9/15/21
to py4web
I would like to know how can I integrate socket io with the py4web. I am going through socket io documentation (link to socket io server documentation) and found below line would do it.
app = socketio.WSGIApp(sio, app)                                                

But in py4web I don't know how to access the app instance. Is there any tutorial that shows how to integrate socket io with existing py4web app?

Alexander Beskopilny

unread,
Sep 15, 2021, 2:29:03 AM9/15/21
to py4web
Here's a working py4web-socketio-application
https://github.com/ali96343/lvsio
( The application is fully compliant with the python-socketio-documentation )
---------------
for string from your question app = socketio.WSGIApp(sio, app)
you can see lvsio equivalent 

r_url = "redis://"
r_mgr = socketio.AsyncRedisManager(r_url, write_only=False)

sio = socketio.AsyncServer(
async_mode="asgi",
client_manager=r_mgr,

cors_allowed_origins="*",
SameSite=None,
logger=True,
engineio_logger=True,
)
app = socketio.ASGIApp(sio, static_files={"/": "./public/index.html"})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
it's equivalent for your question-string
------------------------------------------------------------------------------------------------------------------------
If you don't like the intermediate-channel-socketio server
you can integrate socketio-events into the internal-py4web-server

for "how to integrate" look into examples 

Kevin Keller

unread,
Sep 15, 2021, 3:16:46 AM9/15/21
to Alexander Beskopilny, py4web
Thanks Alex, 

I have used the websocket server with py4web and it works great. 

Really appreciate you sharing this. 



Alexander Beskopilny

unread,
Sep 15, 2021, 4:41:01 AM9/15/21
to py4web
To integrate the socketio (or websocket) into the py4web-app
pay attention to the data flow in the sio-ws-application

1. the py4web-controller sends an html page to the browser
 with the data from the pydal and the sio-connection-string-to-the-sio-server

2 the sio-server sends the data to a specific controller via POST request
(or via json-interface to pydal  bypassing the sio-server )
( The sio-server captures all sio-events-communication with the browser )

3 the py4web-wsgi-controller sends an event to  sio-server via redis connection _only_
( or sends the html-page to the browser bypassing the sio-server )

4 the py4web-controller and the sio-server can also communicate
through additional redis channels with py4web-controllers-fixture
-----------------------------------------
5 it is also convenient to use the socketio to interact with the celery
6 The main source of information about the socketio is Miguel Grinberg 

Kevin Keller

unread,
Sep 15, 2021, 4:44:36 AM9/15/21
to Alexander Beskopilny, py4web
This is super cool!! 

Thanks again. 

I had another thought though, how hard would it be to make ombott (our bottle replacement) async using and runig with e.g. uvicorn?

Would that not also simplify our 2 server architecture? 



Alexander Beskopilny

unread,
Sep 15, 2021, 5:50:47 AM9/15/21
to py4web
(sorry for typo in sentence 2 in prev-post , it should be - (or via json-interface to pydal )     )

Thanks for the question.

It seems to me that by running py4web,  redis,  celery,  sio-channel-server under 
monitors  (monit, daemontools, etc...) we can have a very usable
analog of  the  erlang machine.

Also, I think it is very difficult to integrate multiple events
of many py4web-applications into one server. 
It is much easier to have independent-sio-channel-server for each py4web-app

of course it is possible to replace sio-uvicorn-channel with other servers - tornado or nodejs .....

( for me, well tested py4web-threading  ,
much better than experimenting with python-async-await ... etc    )
Reply all
Reply to author
Forward
0 new messages