[Feature request] Real-time Event Handling via MQTT
Is your feature request related to a problem? Please describe. Currently, we have to rely on polling various endpoints in a loop if we want to simulate fetching real-time updates, such as new direct messages, post/story likes, comments etc. This approach can be inefficient, lead to delays in receiving information, and may easily hit rate limits, making it hard to build interactive applications or bots.
Describe the solution you'd like We propose integrating real-time event handling into instagrapi by implementing a client for Instagram's internal MQTT service. This would allow the library to receive push notifications for a wide range of events, including direct messages, typing/seen indicators, and changes in other users' online status.
A good reference for this is the TypeScript library https://github.com/nerixyz/instagram_mqtt. It implements both the Realtime and facebook notification system clients.
We propose porting the logic from this library to Python and integrate it into instagrapi.
We could implement this by adding a mixin which provides real-time related API. It could look something like this:
client = Client()
# ... login
# Define callback handlers
def on_dm(event):
"""Callback for when a new direct message is received."""
print(f"Direct message received:\n{event}\n")
def on_typing(event):
"""Callback for typing indicators."""
print(f"User {event.user_id} is typing in thread {event.thread_id}")
def on_like(push):
"""Callback for any FBNS push notification, e.g., a post like."""
print(f"Received a push notification: {push.message}")
# Register the callbacks
client.realtime_on_direct_message(on_dm)
client.realtime_on_direct_update(on_typing) # Handles typing, seen receipts, etc.
client.realtime_on_fbns(on_like) # Handles general push notifications (likes, comments, etc.)
# Methods to send real-time actions
client.realtime_send_direct_message(thread_id="12345678", text="Hello World")
client.realtime_send_direct_reaction(thread_id="12345678", item_id="87654321", emoji="🔥")
# The client would then maintain a persistent connection in the background (e.g. run in another thread)
client.realtime_connect()
# Disconnect the realtime client when things are done
client.realtime_disconnect()
Describe alternatives you've considered We could continue with the current polling mechanism, but it's not a viable long-term solution for real-time applications as it creates latency.
Additional context Related: #2204 #2155 #1989 #223 #2193
@subzeroid could we get some feedback on this? Would be happy to submit PRs.
I was able to replicate. But only shows online, not able to receive SMS. working on it
I solved it.
How you solved? can you share code?
How you solved? can you share code?
It is a secret
which library's used?
no library, do it by myself.
which python modules used? I used SSL, websockets. Not working for me
I was able to get live messages from mqtt with version 344.
I was able to get live messages from mqtt with version 344.
I was able to get live messages from mqtt with version newest
I was able to get live messages from mqtt with version 344.
I was able to get live messages from mqtt with version newest
It's same for version above from 344
You can reference to https://github.com/Kunboruto20/nodejs-insta-private-api too