diff --git a/docs/rpi-documentation/websockets.md b/docs/rpi-documentation/websockets.md index ff80e29..c5a612e 100644 --- a/docs/rpi-documentation/websockets.md +++ b/docs/rpi-documentation/websockets.md @@ -1,3 +1,14 @@ # Websockets -## Introduction \ No newline at end of file +## Introduction +### Why are we going to use websockets? +With websockets you can establish a connection between the client and the server, and both can send data fast to each other. This is useful for real-time applications like for our project to plot live graphs of the data we are collecting from the sensors. + +### How to use websockets +There are different languages to serve websockets with, but we are going to use Python with the library `websockets`. This library is easy to use and has a lot of documentation. + + + + +#### Sources: +* https://websockets.readthedocs.io/en/stable/index.html \ No newline at end of file diff --git a/server/Websocket.py b/server/Websocket.py new file mode 100644 index 0000000..407038c --- /dev/null +++ b/server/Websocket.py @@ -0,0 +1,21 @@ +import websockets; +import asyncio; + + +async def handler(websocket): + while True: + #Save the incoming message in variable message. + message = await websocket.recv() + print(message) + + +async def main(): + async with websockets.serve(handler, "145.92.8.114" , 8001): + await asyncio.Future() # run forever + + +if __name__ == "__main__": + asyncio.run(main()) + + +#https://websockets.readthedocs.io/en/stable/reference/sync/server.html#websockets.sync.server.serve \ No newline at end of file