XMPP joint between celery workers

My web application should be able to send XMPP (Facebook Chat) messages, and I thought celery might be a good solution for this. The challenge is to query the database and send an XMPP message to multiple users. However, with this approach, I had to connect to the XMPP server every time I run the task, which is not a great idea.

From the Facebook API API :

Best practics

  • Your Facebook Chat integration should only be used for sessions that are expected to be durable. Customers should not quickly select and disconnect.

Is there a way to share an XMPP connection between workers, so I don’t need to reconnect every time I want to send a message? Or is there a better solution?

+7
source share
2 answers

You can create a worldwide connection in your target celery module and use it from your tasks to send messages. In this case, the connection will be established at startup and will be shared between workflows.

import socket from celery.task import task s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 9999)) @task def echo(arg): s.send(arg) return s.recv() 
+4
source

What about a long-running background job whose task would be to receive messages from other short-lived processes and push them to the XMPP socket?

0
source

All Articles