# Websockets

## 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.

## Issues

Not all ports are open so we need to make a reverse proxy to use websockets.

### Ports
We can only use certain ports like 113, 80, 443. These are common ports and are not blocked by firewalls. Something in the network is blocking all other ports which makes it hard to use websockets.

A solution for this is to use a reverse proxy. (See [Reverse Proxy](./Reverse-Proxy.md))

## Classes

For the websockets we are going to use 2 classes. One for the nodes and one for the websites. The nodes are going to send data to the website and have multiple variables like temperature, humidity, eCO2, TVOC and sound. The website is going to send data to the nodes like names and settings. For this they have to use a userName and password.

``` mermaid
classDiagram

client --> User : website client
client --> Node : esp32 client

namespace raspberry pi clients {
    class client {
        +MacAdress
        sendData()
    }

    class Node {
        +eCO2
        +Temperature
        +Humidity
        +TVOC
        +Sound
        +Settings
        changeNodeName(name)
        updateData(data)
    }

    class User {
        +userName
        +password
        changeNodeName(data)
    }
}
```

Code of the classes:

``` python
class client:
   def __init__(self, macAdress):
      self.macAdress = macAdress

class node(client):
   def __init__(self, name, node, temperature, humidity, eCO2, TVOC, sound):
      super().__init__(macAdress)
      self.nodeNumber = node
      self.temperature = temperature 
      self.humidity = humidity
      self.eCO2 = eCO2
      self.TVOC = TVOC
      self.sound = sound
      self.name = name

   def updateData(self, temperature, humidity, eCO2, TVOC, sound):
      self.temperature = temperature
      self.humidity = humidity
      self.eCO2 = eCO2
      self.TVOC = TVOC
      self.sound = sound

class Website(client):
   def __init__(self, macAdress, user, password):
      super().__init__(macAdress)
      self.passWord = passWord
      self.userName = userName
``` 

#### Sources: 
* https://websockets.readthedocs.io/en/stable/index.html

**Written by Sam & Sietse**