Skip to content

📡A tiny ASGI framework to build both tiny websockets server and tiny websockets client.

License

Notifications You must be signed in to change notification settings

ArtyTheDev/tinyws

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tinyws 📡

a tiny tiny tiny lib to build websocket server using the ASGI server protocol.

Server Usage.

An example usage of it.

import tinyws
import tinyws.server

@tinyws.server.app()
async def application(request: tinyws.server.WebSocket):
    # Accept the connection.
    await request.accept()

    # While loop for the keep-live connection.
    while True:
        try:
            # read the data.
            recv = await request.receive_text()
            print(recv)
        except tinyws.server.WebSocketDisconnect:
            break

Client usage.

import tinyws
import tinyws.client
import asyncio

async def main():
    # To create a connect you can use
    # tinyws.client.Connect(...)
    # they are both the same thing.
    ws = await tinyws.client.Connect("ws://localhost:8000/")

    # To send a packet to the server.
    await ws.send("Hi!")

    # The event loop to stay the connection alive.
    while True:
        # To get the message from the queue.
        message = await ws.receive()

        # PacketType to see what's type of message in
        # the queue.
        if message.packet_type is tinyws.client.PacketType.TEXT:
            print(message)
        elif message.packet_type is tinyws.client.PacketType.BYTES:
            print(message)
        elif message.packet_type is tinyws.client.PacketType.CLOSE:
            break

asyncio.run(main())

you can run it then using any ASGI server like uvicorn just like

$ python -m uvicorn app:application

About

📡A tiny ASGI framework to build both tiny websockets server and tiny websockets client.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages