---
description: >-
  convert any WSGI compatible web framework to Websocket+HTTP framework using
  middleware.
---

# Middleware

**works with many WSGI compatible servers**

**can used with any** [**WSGI**](http://www.wsgi.org/) **compatible web framework**

> Flask, Django, Pyramid, Bottle, ... supported

This middleware adds [`wsgi.websocket`](https://github.com/Ksengine/WSocket/tree/master/docs/websocket.md) variable to [WSGI](http://www.wsgi.org/) environment dictionary.

example with [bottle](https://github.com/bottlepy/bottle):

```python
from bottle import request, Bottle
from wsocket import WSocketApp, WebSocketError, logger, run
from time import sleep

logger.setLevel(10)  # for debugging

bottle = Bottle()
app = WSocketApp(bottle)
# app = WSocketApp(bottle, "WAMP")

@bottle.route("/")
def handle_websocket():
    wsock = request.environ.get("wsgi.websocket")
    if not wsock:
        return "Hello World!"

    while True:
        try:
            message = wsock.receive()
            if message != None:
                print("participator : " + message)
                
            wsock.send("you : "+message)
            sleep(2)
            wsock.send("you : "+message)
            
        except WebSocketError:
            break
            
run(app)
```

{% hint style="info" %}
you should use HTTP version `1.1` Server with your [WSGI](http://www.wsgi.org/) framework for some clients like Firefox browser
{% endhint %}

**for examples on other web frameworks visit** [**`examples/frameworks`**](https://github.com/Ksengine/WSocket/tree/master/examples/frameworks) **folder**

## WSocketApp

```python
WSocketApp(
   app,
   protocol
)
```

### Class variables

`GUID` - unique ID to generate websocket accept key

`SUPPORTED_VERSIONS` - 13, 8 or 7

`websocket_class` - `"wsgi.websocket"` in WSGI Environ
