Pregunta

Lets consider i have 1000 logged-in users in one of biz app. now if one of user xyz send the request to the server but server needs/taking 5-10 mins to complete/returning that request response.

NOTE: As my app running on Google App Engine (Python), this would raise deadline exceed error.

In that case am calling here task/background taskqueue. but client doesn't comes to know whether that requested task status gets completed or not.

So to get the status am trying implement pushing feature (3rd party) using like PubNub or Pusher.

Now the concern here is , How can i publish a message to that requested client xyz only?

NOTE: Server publish status message should be alert/notify to that requested client xyzonly, not to all logged-in users present in that channel.

As am newbie over over here for PubNub or Pusher.

So any idea/logic to cover this kind of scenario.?

Tech NOTE: Google App Engine Python, Javascript.

UPDATE:

Which plan of pubnub (sandbox tier, etc..) do we need?

¿Fue útil?

Solución

Server-Sent Push Notification on Google App Engine using TaskQueues for Background Processing Long Running Tasks

This is the source code you need in your Google App Engine Python solution for notifying the user when using Google App Engine task/background taskqueue has completed a step in the TaskQueue processing steps and also when the TaskQueue has been totally completed. But wait! You want to only send the notification to the Specific User who initiated the TaskQueue request.

Step 1 - How can I publish a message to client XYZ only?

This is easy and Here is the source code:

Also to secure your app, make sure you get proper keys via: https://admin.pubnub.com dashboard.

JavaScript

<script src=https://pubnub.a.ssl.fastly.net/pubnub-3.4.3.min.js></script>
<script>(function(){

    var pubnub = PUBNUB.init({ subscribe_key : 'demo', ssl : true });
    pubnub.subscribe({
        channel : '0IHM4b2VpamZhaWU0eThyaWpvaWdma3Mg',  // SESSION ID
        message : function(message) { alert(message) }  // ALERT MESSAGE
    });

})();</script>

Note that we are using a simple JavaScript Alert() function for showing the message, however you will instead want to turn that into a more robust <div>{message-here}</div> notification area on your page.

TaskQueue Setup

taskqueue.add(
    url='/my-long-task',                                                                              
    countdown=1,
    method='GET',
    params={ 'sessionid' : '0IHM4b2VpamZhaWU0eThyaWpvaWdma3Mg' }
)

Basically you are using the User Session ID to route the communication to user XYZ. Now in Google App Engine Python, you will publish using the Session ID any message state you want to the client.

Google App Engine - Python Task

import webapp2
from Pubnub import Pubnub ## Download - https://raw.github.com/pubnub/pubnub-api/master/google-app-engine/python/Pubnub.py
pubnub = Pubnub( "demo", "demo" )

def server_to_client_notify( sessionId, message ):
        pubnub.publish({
            "channel" : sessionId, ## SESSION ID
            "message" : "hi!"
        })

class LongRunningTaskQueue(webapp2.RequestHandler):
    def get(self):
        ## GET Session ID
        sessionId = urllib.unquote(self.request.get( 'sessionid', '' ))

        server_to_client_notify( sessionId, "Starting Your Job" )
        ## - DO A LOT OF WORK - ##
        ## - DO A LOT OF WORK - ##
        ## - DO A LOT OF WORK - ##

        server_to_client_notify( sessionId, "Your Job is Nearly Complete" )
        ## - DO A LOT OF WORK - ##
        ## - DO A LOT OF WORK - ##
        ## - DO A LOT OF WORK - ##

        server_to_client_notify( sessionId, "Your Job has Finished!" )

app = webapp2.WSGIApplication([('/my-long-task', LongRunningTaskQueue)])

The basic concept is that you Post the SESSION ID along with the TaskQueue URL command which starts/adds the task into the Queue. Then when the TaskQueue starts, you can grab the URL params.

In case you were wondering if there will be more than 1 step, the answer is: "There is only one step."

Otros consejos

The best way to target a specific user with a message is to have the user subscribed to their own unique channel. You can then restrict who can subscribe to that channel:

Pusher doesn't allow you to have multiple users subscribed to the same channel but to send a message/event to only one of those users. As far as I'm aware PubNub don't offer this functionality either.

Neither of the services restrict how many channels you can subscribe to so having a channel per user is generally a good solution:

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top