Use websockets server and client at same time, posible?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
enavas
Posts: 2
Joined: Sun Jan 17, 2021 10:23 pm

Use websockets server and client at same time, posible?

Post by enavas » Sun Jan 17, 2021 10:29 pm

Hi, I want use a full duplex web communication using websockets, I want create a Server (web config interface) and cliient (report status to other server), that is posible??

Or can I have a websocket Server and user a simple curl request (to main server)??

Thanks

marcidy
Posts: 9
Joined: Sat Dec 12, 2020 11:07 pm

Re: Use websockets server and client at same time, posible?

Post by marcidy » Mon Jan 18, 2021 5:05 am

Yes, especially if you use asyncio. I currently am doing this exact thing on v1.13, after modifying an existing websocket implementation to work with the new uasyncio, and writing a very limited http server. The http server serves a static page with jquery which creates a ws connection back to the device. The device will also reach out to a websocket server if it's connected to the net. All run at the same time.

The biggest issue I had was i couldn't find an adequate http server implementation for my needs, but writing a one that worked wasn't difficult. I had to implement chunked transfers to manage the limited memory, but it worked out fine. Can serve arbitrary files (e.g. jquery.min is 89k) in 1024 byte chunks.

marcidy
Posts: 9
Joined: Sat Dec 12, 2020 11:07 pm

Re: Use websockets server and client at same time, posible?

Post by marcidy » Mon Jan 18, 2021 9:00 am

I pulled this example out of a private project. I wouldn't call this a solution as much as a (hopefully) functional example.
https://github.com/marcidy/micropython- ... webexample

The README explains a bit.
95% of the effort is work done by others on this forum. I mashed Peter Hinch's asyncio tutorial and danni's uwebsockets together and everything Just Worked(tm). All thanks to them and the others on the micropython team.

Sorry it's a mess and currently poorly documented. If you are familiar with the async paradigm, you're probably more familiar with how it works than I am :).

I haven't included ssl/tls yet. It "should" work fine, but my first naive attempts failed to interop with other clients/servers as they could not find common ciphers, which probably means I'm not dong something correctly. If i get it to work I'll update the example.

good luck, hope this helps.

enavas
Posts: 2
Joined: Sun Jan 17, 2021 10:23 pm

Re: Use websockets server and client at same time, posible?

Post by enavas » Mon Jan 18, 2021 9:11 pm

Thats looks great!, I'm new using uPython (not Python). I tryed to do this with arduino, but doesnt work.
I will need a secure connection (https or tls), so Can I have a WS server secure and do a secure CURL using upython?
U tryed that before?

Thanks for your support..

marcidy
Posts: 9
Joined: Sat Dec 12, 2020 11:07 pm

Re: Use websockets server and client at same time, posible?

Post by marcidy » Tue Jan 19, 2021 3:26 am

enavas wrote:
Mon Jan 18, 2021 9:11 pm
Thats looks great!, I'm new using uPython (not Python). I tryed to do this with arduino, but doesnt work.
I'm not sure what you mean by with Arduino? Do you mean Arduino IDE, or do you mean an Arduino board with an ESP32 MCU?
enavas wrote:
Mon Jan 18, 2021 9:11 pm
I will need a secure connection (https or tls), so Can I have a WS server secure and do a secure CURL using upython?
U tryed that before?
I haven't been able to get it to work, but this is my first time working with TLS/SSL directly. My server supports TLS fine, but when I try from micropython, I get an error related to no shared cipher suites. I haven't resolved this yet.

I'm pretty sure you can open a socket, wrap it with ussl.wrap_socket, and make any request you want. If you look at where the socket is actually opened, you can wrap it right then. Sorry I don't have an example for you, but there are some available.

Post Reply