Interconnecting AMQP and XMPP - python

I'm looking for a way to write a XMPP bot that would listen to a RabbitMQ queue and send messages to the XMPP channel notifying users of any new issues ( already got Nagios sending notifications to RabbitMQ).
I've tried using xmppy and it stopped working and I stumbled across SleekXMPP which looks fairly better.
I'm just wondering if I define a AMQP listener to automatically call the XMPP "send" method in the bot. So it would be listening both on AMQP and XMPP at the same time.
Thank you for your help!
Edit: Would BOSH be much better solution here ?

The most interesting part of your solution will be that many libraries in this space assume that they are the only event loop. You'll either need to put each in its own thread (seemingly easier, but fraught with lurking locking issues), use a non-blocking I/O approach like Twisted (but you'll need an AMQP library), or extract the socket file descriptors out of each of the libraries you're using and run select() or poll() over them to tell when there is data to read. Of these three, the Twisted approach seems easiest to me.
BOSH will just make the problem more difficult. Don't go that way.

This is really quite simple. I suggest that you start by writing an AMQP listener that simply prints out received messages. Once you get that working it should be obvious how to integrate that into an XMPP bot.

You can use ejabberd and a xmpp plugin like this https://github.com/rabbitmq/rabbitmq-xmpp

Related

Sending Message to user/group of users with uwsgi websockets

Recently I've been doing a lot of testing around different ways of serving our Django application. I've settled on uwsgi as it seems to fit our needs pretty well.
I've recently discovered that uwsgi also supports WebSockets and started looking into it and found some examples: https://github.com/unbit/uwsgi/blob/master/tests/
After running the example (websockets_chat.py) and taking a look through uwsgi's documention for their websockets implementation it appears as though you can only send broadcast, or global messages.
Has anyone managed to find a way to transmit a message to a particular user or does uwsgi not support that level of communication yet?
Cheers
There is nothing like broadcast or global messages in websockets specs. They only "upgrades" an http connection to a lower-level one. What you do with that connection is up to you. The examples show integration with redis as message exchanger but you are free to make other uses.
For your specific case you will need to build a shared list of connected users and implements routing. Remember, you cannot rely on node.js way as it is based on a single threaded setup so everything is way simpler. In uWSGI a websocket connection can happens on a thread, a process or a coroutine, so exchanging data between them is the key.

Python tcp socket client

I need to have a tcp socket client connected to a server to send data and receive.
But this socket must be always on and i cannot open another socket.
I have always some data to send over the time and then later process the answer to the data sent previously.
If i could open many sockets, i think it was more easy. But in my case i have to send everything on the same socket asynchronously.
So the question is, what do you recommend to use within the Python ecosystem? (twisted, tornado, etc)
Should i consider node.js or another option?
I highly recommend Twisted for this:
It comes with out-of-the-box support for many TCP protocols.
It is easy to maintain a single connection, there is a ReconnectingClientFactory that will deal with disconnections and use exponential backoff, and LoopingCall makes it easy to implement a heartbeat.
Stateful protocols are also easy to implement and intermingle with complex business logic.
It's fun.
I have a service that is exactly like the one you mention (single login, stays on all the time, processes data). It's been on for months working like a champ.
Twisted is possibly hard to get your head around, but the tutorials here are a great start. Knowing Twisted will get you far in the long run!
"i have to send everything on the same socket asynchronously"
Add your data to a queue, have a separate thread taking items out of the queue and sending via socket.send()

ZMQ and Twisted

I'm trying to do something seemingly simple, but I'm not sure how to go about it...
I have an application that publishes messages over a ZMQ PUB socket. I'd like to write a small server that subscribes to these updates and broadcasts them to all connected websocket clients.
This seems simple on the surface, but I don't know how to trigger an event in the twisted reactor when data is received through the subscription. I know ZMQ has an ioloop as well, but it is not the same as twisted...
What's the best way to go about this? I don't have to use twisted, some other python framework will do if something can deal with this better (rpclib?)
If you want to use ØMQ with Twisted, you can use txZMQ. The documentation on the linked page has a couple of basic examples which ought to get you started.

Interprocess messaging between two Python programs

We have two Python programs running on two linux servers. Now we want to send messages between these Python programs. The best idea so far is to create a TCP/IP server and client architecture, but this seems like a very complicate way to do it. Is this really best practice for doing such a thing?
I like zeromq for simple messaging, it's really lightweight and fast...very flexible as well. Using AMQP messaging isn't a bad idea either depending on the specifics of your situation, I've found kombu to be a very nice pythonic library for that. You could also use xmlrpclib or setup a simple REST API with bottle or flask. Every option has it's place, so I'd investigate all your options.
This really depends on the kind of messaging you want and the roles of the two processes. If it's proper "client/server", I would probably create a SimpleHTTPServer and then use HTTP to communicate between the two. You can also use XMLRPCLib and the client to talk between them. Manually creating a TCP server with your own custom protocol sounds like a bad idea to me. You might also consider using a message queue system to communicate between them.
You could have a mulitprocessing.managers. As doc says :"A manager object controls a server process which manages shared objects. Other processes can access the shared objects by using proxies."
In your case, you could create a master process that control your other processes, each of those processes will call the master to grab the data.

Proper way to implement a Direct Connect client in Twisted?

I'm working on writing a Python client for Direct Connect P2P networks. Essentially, it works by connecting to a central server, and responding to other users who are searching for files.
Occasionally, another client will ask us to connect to them, and they might begin downloading a file from us. This is a direct connection to the other client, and doesn't go through the central server.
What is the best way to handle these connections to other clients? I'm currently using one Twisted reactor to connect to the server, but is it better have multiple reactors, one per client, with each one running in a different thread? Or would it be better to have a completely separate Python script that performs the connection to the client?
If there's some other solution that I don't know about, I'd love to hear it. I'm new to programming with Twisted, so I'm open to suggestions and other resources.
Thanks!
Without knowing all the details of the protocol, I would still recommend using a single reactor -- a reactor scales quite well (especially advanced ones such as PollReactor) and this way you will avoid the overhead connected with threads (that's how Twisted and other async systems get their fundamental performance boost, after all -- by avoiding such overhead). In practice, threads in Twisted are useful mainly when you need to interface to a library whose functions could block on you.

Categories