Quickstart

This section is a quickstart to the library, explaining how to install it and showcasing some basic examples.

Installing

shinkei is available on PyPi so it can be installed through pip:

pip install shinkei -U

This library is compatible only with Python 3.5.3+ and has two main dependencies, websockets and aiohttp.

It’s also recommended to install ujson for faster JSON encoding/decoding.

The library can be installed with this extra dependency through this command:

pip install shinkei[ujson] -U

Example

This first example showcases some basic communication between a sender and a receiver

Receiver:

# basic imports
import asyncio
import uuid

import shinkei


async def main():
    # async context manager so as soon as the program exists it will close the connection
    async with shinkei.connect("singyeong://localhost:4567", application_id="my-cool-app",
                               client_id=uuid.uuid4().hex, tags=["receiver"]) as conn:
        # set some basic metadata for routing purposes
        await conn.update_metadata({"receiver_id": 1})

        # report every data sent with this async iterator
        async for data in conn.stream("data"):
            print("Received {0.payload} from {0.sender}".format(data))


loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()

Sender:

import asyncio
import uuid

import shinkei


async def main():
    async with shinkei.connect("singyeong://localhost:4567", application_id="my-cool-app",
                               client_id=uuid.uuid4().hex, tags=["sender"]) as conn:
        # target the first non restricted client found which has a receiver_id equal to 1
        target = shinkei.QueryBuilder(application="my-cool-app", key="uniquekey").eq("receiver_id", 1)

        for number in range(10):
            # this will raise if no target is found.
            await conn.send("Hi! (send number {0})".format(number), target=target)

            await asyncio.sleep(5)


asyncio.run(main())

With both of these scripts running, whenever the sender sends a message it will be routed to the receiver and printed, due to it being the only client connected to the same app with a receiver_id equal to 1.