import time
import json


async def upload(ws, filename):
    """Send an upload request followed by a bytes stream on an established websocket connection.

    :param ws: an established websocket connection
    :param filename: the file name to be uploaded
    :return: the Upload response.
       >>> resp['payload']['upload_id']  # contains the upload identifier.
    """
    upload_request = {
        'emit':    "upload",
        'payload': {
            'max_uploads': 1,
            'timestamp':   int(time.time())
        }
    }
    await ws.send(json.dumps(upload_request))
    await ws.send(bytes(open(filename, 'rb').read()))
    return json.loads(await ws.recv())