How to integrate Django API with XMPP server (ejabberd) - python

I'm currently working on a project where I'm using Django rest framework (DRF) for the backend.
I need to implement one to one chat on this application. For this, I'm using XMPP server. I'm using ejabberd as XMPP server.
Now, I need to create users in the ejabberd server using my API built with DRF.
I need to achieve following things with my API:
create new users in the ejabberd server
create rooms in the ejabberd server
fetch all the available rooms.
The username will be fetched or retrived from the frontend.
Is there any Python API client or Django API client to do this in the ejabberd server? Like how there is simple-xmpp for node js
I have seen many python packages for this. some of them are,
pyjabberd
xmppy
django-xmpp
I'm not sure which one to use and I don't know whether is it possible to implement the above using any of these packages.

All those libraries that you mention seem to be to write XMPP clients. In that case, you can write a small XMPP client that will attempt to register account in a xmpp server (being ejabberd or whatever), that joins a MUC room, configures it to be persistent, then requests the lists of rooms...
A completely different approach would be to call API commands that ejabberd provides. As you can see in the API documentation, there are commands to perform precisely what you want, and many more: register,
create_room_with_opts,
muc_online_rooms.
And how to call this API? For playing and checking what those commands do, you can use the ejabberdctl command-line script. Probably you will later want to use ReST (then configure mod_http_api) or XML-RPC (in that case configure ejabberd_xmlrpc). See
Understanding ejabberd commands, and also check the example configuration and example calls using curl.

Related

Using FastAPI for socket chat system?

How would one implement a comprehensive chat system using sockets within FastAPI. Specifically keeping the following in mind:
Multiple Chat rooms many-to-many between users
Storing messages with a SQL or NoSQL database for persistence
Security: Authentication or possibly encryption
I've looked at some libraries, but actual useful implementations are far between, regrettably.
Any advice or redirects towards places for more information will be of great help!
For chat rooms you could use FastAPI builtin websockets support and add redis pubsub or PostgreSQL pg_notify to it for sending messages to all participants in the room.
Storing messages in PostgreSQL is a solid choice because of its long history and stability.
Authentication can be handled by OAuth2 provider in FastAPI. Authorization can be handled by OAuth2 scopes that is hidden in the Advanced Security section in the FastAPI Documentation. Encryption is provided by HTTPS and reverse proxy that you put in front of your app.
There aren't any fully ready made libraries that provide everything out of the box. But breaking down the problem in to smaller pieces and then working on those will get you pretty far.
Write down what fields/data you want to store about your users, chat rooms, messages.
Implement those basic models in FastAPI probably using SQLAlchemy.
Wire up those models to api endpoints so that you can use those models in Swagger (list chatrooms, get and post messages into chatrooms).
Implement a websocket endpoint in FastAPI that will echo back everything sent to it. That should allow you to wire up some client side javascript for sending and receiving messages from the websocket.
Modify your exising message storing endpoint to push the same message also to redis publish topic and change your websocket endpoint to subscribe to the redis subscribe topic.
Add authentication to your endpoints. At first basic user/password, later more advanced configurations.
Add reverse proxy with https in front and voila.

Client-Server framework for python

I'm currently working on a University project that needs to be implemented with a Client - Server model.
I had experiences in the past where I was managing the communication at socket level and that really sucked.
I was wondering if someone could suggest an easy to use python framework that I can use for that purpose.
I don't know what kind of details you may need to answer so I'm just going to describe the project briefly.
Communication should happen over HTTP, possibly HTTPS.
The server does not need to send data back or invoke methods on the clients, it just collects data
Many clients send data concurrently to server, who needs to distinguish the sender, process the data accordingly and put the result in a database.
You can use something like Flask or Django. Both frameworks are fairly easy to implement, Flask is much easier than Django IMO, although Django has a built in authentication layer that you can use, albeit more difficult to implement in a client/server scenario like you need.
I would personally use Flask and JWT (JSON Web Tokens), which will allow you to give a token to each client for authentication with the server, which will also let you differentiate between clients, and you can use HTTPS for your SSL/TLS requirement. It is tons easier to implement this, and although I like django better for what it brings to the table, it is probably overkill to have you learn it for a single assignment.
For Flask with SSL, here is a quick rundown of that.
For JWT with Flask, here is that.
You can use any database system you would like.
If I understood you correctly you can use any web framework in python. For instance, you can use Flask (I use it and I like it). Django is also a popular choice among the python web frameworks. However, you shouldn't be limited to only these two. There are plenty of them out there. Just google for them.
The implementation of the client depends on what kind of communication there will be between the clients and the server - I don't have enough details here. I only know it's unidirectional.
The client can be a browser accessing you web application written in Flask where users send only POST requests to the server. However, even here the communication will bidirectional (the clients need to open the page which means the server sends requests back to the client) and it violates your initial requirement.
Then it can be a specific client written in python sending some particular requests to your server over http/https. For instance, your client can use a requests package to send HTTP requests.

Service management using RESTful requests

I am currently working on a project where we need to establish communication like an ESB, between a REST API and the apps services on a small scale.
Scenario:
Assume a web app front end (e.g. Django/Python or Ruby/Rails) and services that are accessible via a HTTP RESTful request.
How can I:
make it configurable which web services are called on a web request depending on the request and not requiring code changes (through keys for example)
encapsulate or implement the services in a way to make it easy to manage them e.g. start/stop etc.
I have been looking at spring.io, but cant work out whether this could be used for the this??
I am open to all suggestions,
Thanks
From what I understand, you want an authorisation solution.
In Rails, Pundit and CanCanCan are very popular. You could also implement it from scratch. Here is a screencast to help you get started.

Is it possible to use GCM with a python GAE backend?

I have a python GAE service, and I want to push notifications from the server to devices. The tutorial available for GCM is written for Java, and runs on ant+Tomcat/Jetty+JAE. I was under the impression that GCM would be a language-agnostic web service, and that I would be able to send push notifications regarding of my server-side platform.
Was I mistaken about GCM being compatible with my python GAE
backend?
If I CAN use it with my existing server, what instructions
can I follow (or adapt) to get started with sending notifications to
a mobile client?
Sure you can. GCM has a JSON REST API that you can work against. First you need to register you project here: http://developer.android.com/google/gcm/gs.html.
You basically do this:
Acquire you API key from http://developer.android.com/google/gcm/gs.html#access-key
Construct your payload, a dict containing registration_ids, data etc
Using url.fetch https://developers.google.com/appengine/docs/python/urlfetch/ to send the data as a JSON string to the GCM API
Here's another question with some code. Google Cloud Messaging HTTP Error 400: Bad Request and a blogpost (in not english, i think spanish. but there some sample code) http://pforray.wordpress.com/2012/07/05/ejemplo-gcm-en-appengine-python/
Use gcm-client
pip install gcm-client
Reference:
https://pypi.python.org/pypi/gcm-client/
Here you can find a module for Python interface for sending push notifications via Pushwoosh.
https://github.com/dbtsai/python-pushwoosh
You can use it for sending messages via Pushwoosh (it's free) or adapt it for your needs.

RESTful API across multiple users

I am somewhat new to RESTful APIs.
I'm trying to implement a python system that will control various tasks across multiple computers, with one computer acting as the controller.
I would like all these tasks to be divided amongst multiple users (ex. task foo runs as user foo, and task bar runs as user bar) while handling all requests with a central system. The central system should also act as a simple web server and be able to server basic pages for status purposes.
It it possible to have each user register a "page" with a central server for the API and have the server pass all requests to the programs (probably written in Python)?
Sure, you just need the clients to POST their notifications URL to the server, so that the server can then POST them back with the requests. These are called Webhooks by some people.
Also see RESTful Webhooks.
Yes. Keep in mind that being RESTful is merely a way to organize your web application's URL's in a standard way. You can build your web application to do whatever you want.

Categories