Skip to content

avigmati/channels_endpoints

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django channels endpoints

This package provides endpoints as functions for django channels package.

Installation

pip install channels_endpoints

Usage

In your django app create dce.py file:

from channels_endpoints.main import endpoint, Response
from django.contrib.auth.models import User
from django.core import serializers
from .permissions import UsersPermissions
from asgiref.sync import sync_to_async


users_logger = logging.getLogger('users')


@endpoint(logger=users_logger, permissions=[UsersPermissions], timeout=42)
async def get_users(request):
    
    print(request.data)
    
    def _get():
        return serializers.serialize('python', User.objects.all(), fields=('username', 'id'))

    await request.consumer.send(text_data=Response(None, f'hello', consumers=['SomeConsumer']))
    
    return Response(
        request,
        await sync_to_async(_get)()
    )

Python client:

from channels_endpoints.client import Dce, DceException, consumer

@consumer
async def SomeConsumer(response):
    print(f'SomeConsumer: {response["data"]}')
    
dce = Dce('http://127.0.0.1:8000/ws/')

try:
    data = await dce.request('myapp.get_users', {'some': 'data'})
except DceException as e:
    print(e.__repr__())

dce.close()

Javascript client:

import {dce, consumer} from "channels_endpoints"

consumer('SomeConsumer', (response) => {
    console.log('SomeConsumer: ', response.data)
})

dce('myapp.get_users', {some: "data"}).then(
    response => {
        console.log('users:', response)
    }
)

see js client package https://www.npmjs.com/package/channels_endpoints

Examples

For complete usage examples see example chat

About

Django channels endpoints

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages