Skip to content

Simple example of uasyncio WebSocket Server (secure - with SSL) #8177

Description

@beyonlo

Hi all.

I would like a simple example of uasyncio WebSocket Server (secure - with SSL). I see that there is a official uwebsocket module implemented on MicroPython.

>>> import os
>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.17.0', version='v1.17 on 2021-09-02', machine='ESP32 module (spiram) with ESP32')
>>> import uwebsocket
>>>

Well, so I found a example (https://github.com/micropython/micropython/blob/master/extmod/webrepl/webrepl.py) that use the official uwebsocket module. Need just to add SSL + uasyncio on that example.

Thank you in advance!

Activity

  1. Tangerino commented on Jan 16, 2022

    @Tangerino

    You can look here. Not sure about quality, never used.

    https://github.com/jczic/MicroWebSrv
  2. beyonlo commented on Jan 16, 2022

    @beyonlo
    Author

    You can look here. Not sure about quality, never used.

    https://github.com/jczic/MicroWebSrv
    

    @Tangerino

    That use thread, not uasyncio.

  3. bazooka07 commented on Jan 19, 2022

    @bazooka07

    Look at Picoweb. It uses uasyncio by Paul Sokolovsky.
    https://github.com/pfalcon/picoweb
    For ssl, I don't know. But it has an example with basic authentication.

  4. beyonlo commented on Jan 19, 2022

    @beyonlo
    Author

    Hello @bazooka07

    Look at Picoweb. It uses uasyncio by Paul Sokolovsky. https://github.com/pfalcon/picoweb For ssl, I don't know. But it has an example with basic authentication.

    The Picoweb do not support websocket. Even uasyncio on PicoWeb is old, it is not the official v3 uasyncio compatible, but it is good start point.

    The https://github.com/jczic/MicroWebSrv use WebSocket, but is not official uwebsocket module - it wrote their own websocket module. And is better (if is possible) to use the official, for compatibility and so on.

    This project https://github.com/Carglglz/upydev/tree/master/upyutils has a WebREPL example working with SSL using the official uwebsocket module - that is great. This project was put as a PR (#5611) to support SSL in the official WebREPL, but I do not know why still not was merged, after two years.

    I need just to know now how to add uasyncio on that WebREPL + SSL example.

  5. QGB commented on Oct 6, 2022

    @QGB
    'Stream' object has no attribute 'ios'
    
    
  6. Carglglz commented on Dec 22, 2022

    @Carglglz
    Contributor

    @beyonlo FYI with the latest changes to #8968 I'm able to run a microdot_async + tls (https) server and WebREPL + tls (wss) + aiorepl at the same time, so I think the uasyncio WebSocket Server (secure - with SSL) should be possible too. 👍🏼

  7. beyonlo commented on Dec 23, 2022

    @beyonlo
    Author

    @Carglglz Excellent!

    so I think the uasyncio WebSocket Server (secure - with SSL) should be possible too. 👍🏼

    Sorry for my ignorance, but think that already working on the Microdot, like as show this example echo_async_tls.py working with this index.html that use wss (WebSocket Secure Server).

    Please, let me know what I missing. I'm a bit confuse now: how that (uasyncio WebSocket Server (secure - with SSL)) already working on Microdot if the #8968 is a new implementation to have that working?

    Thank you in advance!

  8. Carglglz commented on Dec 24, 2022

    @Carglglz
    Contributor

    @beyonlo I think those examples only work with CPython at the moment?, unless I'm missing something in that example

    import ssl
    from microdot_asyncio import Microdot, send_file
    from microdot_asyncio_websocket import with_websocket
    
    app = Microdot()
    
    
    @app.route('/')
    def index(request):
        return send_file('index.html')
    
    
    @app.route('/echo')
    @with_websocket
    async def echo(request, ws):
        while True:
            data = await ws.receive()
            await ws.send(data)
    
    
    sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    sslctx.load_cert_chain('cert.pem', 'key.pem')
    app.run(port=4443, debug=True, ssl=sslctx)

    Right now MicroPython ssl module has no SSLContext, and in app.run

    This calls

    asyncio.run(self.start_server(host=host, port=port, debug=debug,
                                          ssl=ssl))

    which calls

    try:
             self.server = await asyncio.start_server(serve, host, port,
                                                         ssl=ssl)
     except TypeError:
                self.server = await asyncio.start_server(serve, host, port)

    And if you look at asyncio.start_server in MicroPython extmod/uasyncio/stream.py there is no ssl option

    async def start_server(cb, host, port, backlog=5):

    Also this comment from @miguelgrinberg in the issue you opened in #34

    Would appreciate it if you test it and provide feedback. Note that the async support cannot currently be used with MicroPython, since uasyncio does not have the necessary support

    Well, with the latest changes to #8968 the async support is enabled, so those examples will work in MicroPython too. 😄

  9. beyonlo commented on Dec 24, 2022

    @beyonlo
    Author

    @Carglglz Ohh, you are right, that echo_async_tls.py do not works with MicroPython. As I still developing using as base example just the echo_async.py (WebSocket Server without SSL) and just when the entire application will be stable that I was thinking do add the ssl, that was the reason that I never had tested the echo_async_tls.py on the MicroPython.

    Also this comment from @miguelgrinberg in the issue you opened in #34

    Would appreciate it if you test it and provide feedback. Note that the async support cannot currently be used with MicroPython, since uasyncio does not have the necessary support

    Yes, I remember now about that moment. Maybe a good idea, as suggstion, is @miguelgrinberg add a note in the that README.md (inside tls examples) talking about that the echo_async_tls.py works (for now :) ) just on the CPython.

    Well, with the latest changes to #8968 the async support is enabled, so those examples will work in MicroPython too. smile

    Excellent, so maybe sooner the echo_async_tls.py will works on MicroPython as well :)

    Thank you for the explanation!

  10. added a commit that references this issue on Jul 18, 2023
  11. dpgeorge commented on Dec 14, 2023

    @dpgeorge
    Member

    Asyncio now supports SSL server and client mode. So that's the first and main step to getting secure WebSockets.

  12. Josverl commented on Mar 19, 2026

    @Josverl
    SponsorContributor

    Closing as the question has been answered and the conversation appears to have stopped.

    Please feel free to reopen with additional information or questions as needed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions