Sockjs - Send message to sockjs-tornado in Python code - python

I use https://github.com/mrjoes/sockjs-tornado for a Django app. I can send messages from javascript console very easy. But I want to create a signal in Django and send json string once the signal is active.
Could anyone give me a way to send a certain message in Python to sockjs-tornado socket server?

There are few options how to handle it:
Create simple REST API in your Tornado server and post your updates from Django using this API;
Use Redis. Tornado can subscribe to the update key and Django can publish updates to this key when something happens;
Use ZeroMQ (AMQP, etc) to send updates from the Django to the Tornado backend (variation of the 1 and 2).
In most of the cases, it is either first or second option. Some people prefer using 3rd option though.

I've wrote djazator, simple and easy to use django plugin. It uses zeromq for delivering messages from django to sockjs-tornado. Additionally, it can send messages to subset of authenticated django users.

I just put this up https://github.com/amagee/sockjs-client for talking directly to a SockJS server from Python (using xhr streaming).

Related

Python service/framework to run code

I am having trouble finding an answer to this question.
I am looking for a framework where I can send a python function to a rest endpoint, and it sends a response upon completion.
If there is no precanned solution, what would be a good set of packages to use to do this?
Maybe you could try to use SocketIO with Celery.
SocketIO is an option to create a channel between clients (web, mobile apps, etc) and server.
When received a specific message (you can create you own protocol) you can call a task in a Message Queue using Celery framework with Redis or RabbitMQ.
Maybe you need to use an another framework called Kombu to be able to send a message using the connection in socketio.
You can find some tips on this link: http://python-socketio.readthedocs.io/en/latest/
I hope I could helped.

How to send notifications to GCM using python

I'm searching for a way to send push notifications to devices from my django project. I want to send push notifications using pure python, with out third packages like django-gcm or python-gcm.
It is just an HTTP request to an endpoint. The docs explain how to use it. https://developers.google.com/cloud-messaging/downstream

Push Notification in DRF

What should be exact and most appropriate method to implement push notifications in django rest framework. DRF documentation doesn't explain it well.
Help me out with this one
Django REST framework is for creating API in django using built in django functionalities to retrieve request and serve response.
Django by itself doesn't support any protocol that will allow push notifications to be sent to client, so DRF also won't do that.
To create push notifications you have to use websockets, there are some third-party packages adding support of websockets to django. One of them is django-websocket-redis
If you are looking for push notification on the web (using web socket), you can try to use Firebase Cloud messaging (FCM, Firebase is free with certain usage threshold). What you need is adding code to your django server to request Firebase to send notifications.
If you are looking for push notifications on mobile devices, django-push-notifications package looks good, it even provide support for DRF to keep track of devices. On Android, this will also use FCM as well.
django-push-notifications is the one you are looking for. It is simple and can be implemented with few lines of code. It can be used for all android, iOS and web push notifications.
If you intend to use WebSocket to implement your own notification, you can check out channels
My Personal Favourite for this Purpose is: Django Notification! And Django Pushy!
Other Packages: django-push-notifications Django Activity Stream
Try using the django-instapush package. It is easily available to install via pip. This support all sql databases and also mongoDB via mongoengine to store device information. It has built-in models that can be used to store device information so this will work out of the box. Here is a little tutorial on sending django push notifications using django instapush.
You can Also use:
https://pypi.org/project/django-sns-mobile-push-notification/
You can also try with some other push providers. Infobip Push has some new things, for example you can track users by their locations and send push whenever they are apporaching some areas
If you project DRF was structured without asynchronous process in mind. You can try out django channels rest framework. you can easily pass asynch classes to you viewsets and make your endpoints behave like an asynchronous porcess.

Better way to notify websocket server from Django

I have a web application running Django and a separate websocket server. Both using single Django model. When Django make changes to the model, I want Django to notify websocket server about this changes.
Obviously, the way of simply connecting to websocket server and sending one message is looking bad due to increasing server load through connecting/disconnecting clients for each user form submit and due to websocket concept in general.
I've heard about solutions using AMQP server for similar purposes. The question is: is that a good idea, or there are better solutions in my case?
Have a look at https://github.com/jrief/django-websocket-redis
Documentation: http://django-websocket-redis.readthedocs.org/en/latest/

WebSocket + Django python WebService

I was wondering how to create a django webservice (responds with XML) with websockets.
I have already a django webservice which accepts xml requests, parse those requests, makes a database query, creates a response xml and send that xml back to the requester/browser. Just a normal HTTP XML request, where the response is shown as xml within the browser.
But how would i now create a websocket django webservice? Lets say i would like to send a xml response to the requester/browser with the latest data from database whenever a new magical event occurs.
I have read a lot of posts and blogs but it was kinda all too general. Can i solve this only with django + apache or do i need something else next to django and another server only to handle websockets?
I am right now using django 1.3, Apache + wsgi but i would be ready to switch any configuration that would work.
Update:
There are many possible websockets out there,
http://pypi.python.org/pypi?:action=search&term=websocket&submit=search
but which one could be used in my case?
Sorry but django handles async requests very very poorly as it is wsgi. You'll be limited by your number of parallel instance if you have to handle real users. The best solution is to use tornado or node.js.
Tornado handles websocket and long polling brilliantly. Here is my wrapper to allow getting user and sessions from a parallel tornado thread:
https://gist.github.com/1939836
It's adapted from a more complex source, I didn't tested this gist, it's long polling but tornado handlse WebSocket as well.
http://www.tornadoweb.org/documentation/websocket.html
update:
Avoid django-websocket for production use. Even the main developer recommends against it.
I recommend Tornado because it's an awesome technology which is damningly faster/lighter than django. It may be useful for some simple cases. You'll need to configure apache/nginx anyway so, at least get "faster web pages" feature available.
Django-Desktop-Notification focuses on chrome browser and require node.js.
update (01/2016):
Mozilla gave money to django in late 2015 to solve this particular issue, the current most promizing implementation made by a django core dev is this one:
https://github.com/andrewgodwin/channels
It will probably be part of django 1.11 or 2.0
Although it's a bit complicated to setup (but probably the way to go), you could use gunicorn + gevent + socket.io .
I used this article to guide my way through it.
You might also look at server sent events (the article mentioned above looks at that too). If they suit your needs, it would be a bit easier to set up - since you don't have to set up socket.io and you don't need a client library. One catch though - SSE are not supported in IE.
Yeah, django is not all that great when it comes to asynchronous stuffs. My advice for you would be to use twisted as it has a lot of websocket libraries. If you really need to use django..you can make django act just as a pass through, for all the api stuff you build using twisted.

Categories

Resources