millerf / django-channels-jsonrpc Goto Github PK
View Code? Open in Web Editor NEWAdd JSON-RPC ability to channels
Home Page: http://www.millerf.com/#backend/django-channels-jsonrpc
License: MIT License
Add JSON-RPC ability to channels
Home Page: http://www.millerf.com/#backend/django-channels-jsonrpc
License: MIT License
We could use this package http://json-rpc.readthedocs.io/en/latest/index.html (for example) as a dependency and get rid of all protocol-agnostic questions (encoding/decoding/validation). We will not have to think wheater our JSON-RPC is correct (according to the Spec) or not.
While using new outbound notifications in my project, I realized that our current implementation is not a completed solution:
JsonRpcConsumer.notify_group()'s "params" parameter is directly assigned to JSON-RPC "params" without any checks, that may lead to incorrect JSON output. "params" must be forced to list or dict types, at least. But using python's argument unpacking and named parameters we could make it in a better way.
In fact, we a making a JsonRpc client (asuming outbound calls), which is aimed to work ot top of Django Channels. So I think we shoud go the same way as most clients do, using Channels as an endpoint. A standart approach of all clients looks like this:
client = JsonRpcClient(endpoint) # endpoint = some http url or websocket url
client.call(method_name, param1, param2)
client.notification(method_name, param1, param2)
In our case it might look the same way, for example:
JsonRpcClient(Group('name')).notify(method, param1, param2) # notification with positional arguments as per JSON-RPC spec
JsonRpcClient(message.reply_channel).notify(method, param1=value1, param2=value2) # notification with named arguments
So the solution is:
I may implement it tomorrow and make a pull request, it you like this approach.
This code is not thread-safe:
global original_message
original_message = original_msg
result = method(*args, **kwargs)
original_message = None
If you have two JsonRpcWebsocketConsumer
you can get a moment, when one thread reset original_message
and other tries to read it.
It would be great to implement inbound (and outbound) notifications.
I could make a pull-request. I think inbound notifications may be processed by the same __process method (but not return any value, so nothing will be sent back) and handlers may be defined with the same JsonRpcWebsocketConsumer.rpc_method() decorator... what do you think?
By outbound notifications i mean possibility to send JsonRpc notifications to "reply_channel" or to Groups (make implementation bi-directional in some way). I think a good starting point would be to create some helper method like "create_notification(method, params)" that just takes method name and params and returnes a correct JSON structure defined in JsonRpc protocol, so that it may be used like this:
original_message.reply_channel.send({"text": create_notification(method, params)})
Group("xxx").send({"text": create_notification(method, params)})
Hej Guys!
You did a nice work with that jsonrpcconsumer... Just one question. I tried to integrate your package into my already build up project and following your documentation i realized that the link to channels is dead.
Further i realized that your requirements need channels==1.1.6 whilst the latest stable version is around 2.1.1.
So my question, did you already tested agains channels>=2.1.0
kine regards, Mr. J
Hi.
What about implementing another consumer, for http requests in the same fashion as websocket?
What is the reason for executing JsonRpc in separate threads?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.