0

Notification poll comet-style

Hello, i have successfully manage to get the periodic poll for notifications. Now i want my app to get real time messages from yahoo sao i need to implement the comet-style polling.
I do the client in java for Android so i have the following code:

CODE
Random rand = new Random(System.currentTimeMillis() / 1000);

String link3 = ""; // the link

link3 = "http://"+NOTIFY_SERVER+"/v1/pushchannel/"+USERNAME+"?";
link3 += "oauth_consumer_key=" + CONSUMER_KEY;
link3 += "&oauth_nonce=" + rand.nextDouble();
link3 += "&oauth_signature_method=" + "PLAINTEXT";
link3 += "&oauth_timestamp=" + (System.currentTimeMillis() / 1000);
link3 += "&oauth_token=" + OAUTH_TOKEN;
link3 += "&oauth_version=" + "1.0";
link3 += "&oauth_signature=" + CONSUMER_SECRET + "%26"+ OAUTH_SECRET;
link3 += "&sid=" + SESSION_ID;
link3 += "&seq=" + notifId;
link3 += "&count=100";
//link3 += "&c="+CRUMB; // i think this is not good here
link3 += "&idle=120";
link3 += "&rand="+ rand.nextDouble();
link3 += "&IM="+IM_COOKIE;

try {

HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet(link3);

//get.setHeader("Cookie","IM="+IM_COOKIE);
get.setHeader("Connection","keep-alive");

HttpResponse rp = hc.execute(get);

Log.i("GET MESSAGE HEADER NEW STYLE", "" + rp.getStatusLine().getStatusCode());

if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream is = rp.getEntity().getContent();

BufferedReader reader = new BufferedReader(
new InputStreamReader(is));

// get the response and manage it

}

}

}

} catch (Exception e) {
Log.e("GET MESSAGE FAILED", "" + e);
}


With this code i get 400 error code "Bad format", i think the problem is the cookie, i receive it somehow expired...
CODE
 set-cookie: IM=2pFowmPXZO.9Kbgvj02Ead_4V.sw021oxTP7q5n8ttaqsd8t0_wiEGH4zxJYYAGGeI3YDB_5m3vxX
SvU-|IK1pqqUj_OUjjOPh3U4QGQ--; expires=Thu, 02-Dec-2010 21:19:48 GMT; path=/; domain=.messenger.yahooapis.com

And the request time for the cookie is at the same our...

the session request from witch i get the cookie it's made like this:

CODE
Random rand = new Random(System.currentTimeMillis() / 1000);

String link3 = "http://developer.messenger.yahooapis.com/v1/session?";
link3 += "realm=" + "yahooapis.com";
link3 += "&oauth_consumer_key=" + CONSUMER_KEY;
link3 += "&oauth_nonce=" + rand.nextDouble();
link3 += "&oauth_signature_method=" + "PLAINTEXT";
link3 += "&oauth_timestamp=" + (System.currentTimeMillis() / 1000);
link3 += "&oauth_token=" + OAUTH_TOKEN;
link3 += "&oauth_version=" + "1.0";
link3 += "&oauth_signature=" + CONSUMER_SECRET + "%26"+ OAUTH_SECRET;
link3 += "&notifyServerToken=1";

HttpClient hc = new DefaultHttpClient();
HttpPost get = new HttpPost(link3);

String st = new String("" + "{" + "\"presenceState\" : " + STATUS
+ "," + "\"presenceMessage\" : \"" + PRESENCE + "\"" + "," +
"\"clientCapabilities\" :" + "[" + "{"
+ "\"clientCapability\" : \"" + "smiley" + "\"" + "},"
+ "{" + "\"clientCapability\" : \"" + "buzz" + "\"" + "},"
+ "{" + "\"clientCapability\" : \"" + "fileXfer" + "\""
+ "}" + "]" +
"}");

get.setHeader("Content-type", "application/json; charset=utf-8");
get.setHeader("Connection", "Keep-Alive");

HttpEntity content = null;

content = new StringEntity(st);

get.setEntity(content);

HttpResponse rp = null;
rp = hc.execute(get);

String cookie = rp.getFirstHeader("set-cookie").getValue();

int i = 3;

// get the cookie without the "IM=" string and replace the "|" symbol with "%7C" for uri encoding

while (!cookie.substring(i, i+1).equals(";")){

if (cookie.substring(i, i+1).equals("|")){
IM_COOKIE += "%7C";
}else {
IM_COOKIE += cookie.substring(i, i+1);
}

i++;

}


What is wrong in my request? or what do i have to do to get the comet-style polling to work?
Please help me!

Thank you very much!

38 Replies
  • I also faced the same problem. But i am using C# language.

    It works fine while using the POlling, but when using the comet-style, it generates the error for the cokie similar to the error that you are getting.

    has anyone got their app to work with the comet-style API?
    0
  • Hi Prata

    Could you paste your HTTP Request Header in here please?

    Thanks
    ~jimmi
    0
  • Sorry for posting so delayed but I have some job to do :DThank you!
    0
  • Do not escaped the IM you received, just send it as it is. As I can see your IM then the character | was changed to %7C, this is not correct. Another point (but not important), seq number is a integer number.

    BTW, I'm doing a C# Application to use Yahoo Messenger SDK and having problem with Comet - Style request. I could send request and receive the response, however I can only receive the response ONCE. I have no way to handle the event (is it an event?) when the server push message to the client. Any suggestion for me?

    Thanks so much.
    0
  • Thank you for the answer,
    I have replaced the | char with that code because httpget class from java don't let me send a string with that character, is there another way that I could send that request without the "|" symbol?
    Thank you!


    NgAc Long, did you tried to make another request after the first request?
    I think you need to make another request after you get a answer from the server...
    0
  • I think you should put IM in Cookie instead, then you will not need to escape that character. In my side I put that in Cookie and it works well.

    BTW, after hours dealing with Comet Style Push, I finally get it through. Thanks god.

    Actually I tried to send another request after received the request from the server but I always get the server response immediately the same content with previous response. This makes me thought that Comet is not working.
    By chance while I'm reading about Comet over the Internet, some pages mention sequence number and all I could think at that time is why don't I tried increase this sequence number for each request. And what a lucky, It worked like a charm.

    Till now I have done a simple chat client, that can log in / out, sending and receiving message.

    Thank you.
    0
  • That's great to know. I am also doing a C# project, and can successfully login, fetch the buddy list and logout.
    Learning about JSON took some time, and finally I was able to receive the contacts list in Json. I am also not sure how to login as invisible, and have posted a question in the forums.

    Could you share some code that shows
    1. notifications/comet,
    2. how to get the contact list to display, bound to a control,
    3. login as invisible?

    for contacts handling, I had to create 3 class to handle the Json contact list: ContactsCollection (this holds the Contacs[], start, count etc), Contacts (hold the Contact) and Contact (holds id and uri).

    Thanks
    0
  • Hi again,
    longbkit thanks for your answer again, do you send the cookie in the folowing format:

    IM=2pFowmPXZO.9Kbgvj02Ead_4V.sw021oxTP7q5n8ttaqsd8t0_wiEGH4zxJYYAGGeI3YDB_5m3vxX
    SvU-|IK1pqqUj_OUjjOPh3U4QGQ--

    or

    set-cookie: IM=2pFowmPXZO.9Kbgvj02Ead_4V.sw021oxTP7q5n8ttaqsd8t0_wiEGH4zxJYYAGGeI3YDB_5m3vxX
    SvU-|IK1pqqUj_OUjjOPh3U4QGQ--; expires=Thu, 02-Dec-2010 21:19:48 GMT; path=/; domain=.messenger.yahooapis.com

    Thank you for the help, now i am trying to put the cookie in the header I hope it works...

    Hassan K, did you try to log in with the status code -1?
    I didn't try that but if I change the status code to -1 it makes me invisible.
    0
  • Catalin,

    Thank you, that was right in front of me and I didn't see it :)Hassan
    0
  • Hi Catalin

    I sent Cookie as follow:

    IM=2pFowmPXZO.9Kbgvj02Ead_4V.sw021oxTP7q5n8ttaqsd8t0_wiEGH4zxJYYAGGeI3YDB_5m3vxX
    SvU-|IK1pqqUj_OUjjOPh3U4QGQ--

    Did you try this? It should works correctly :-)

    Set-Cookie is just a response from server that indicates client must set the Cookie in the next request.

    Good luck!
    0
  • I am getting following error while trying to do comet-style connections using C#.NET

    "The remote server returned an error: (403) Forbidden."
    Status = ProtocolError

    Please find below the URL that i am requesting for which is generating the above error:

    GET

    http://rproxy1.messenger.yahooapis.com/v1/...L8rwRm0Kb0kIQ--



    Please let me know what i am doing wrong. I have my whole application withing fine without any problems when i use the POLLING method, but i want to use the comet style method which is more real time and also consumes lesser bandwidth and also a neater way to really do this.
    0
  • QUOTE (Harshil Patel @ Feb 6 2011, 09:06 AM) <{POST_SNAPBACK}>
    I am getting following error while trying to do comet-style connections using C#.NET

    "The remote server returned an error: (403) Forbidden."
    Status = ProtocolError

    Please find below the URL that i am requesting for which is generating the above error:

    GET

    http://rproxy1.messenger.yahooapis.com/v1/...L8rwRm0Kb0kIQ--



    Please let me know what i am doing wrong. I have my whole application withing fine without any problems when i use the POLLING method, but i want to use the comet style method which is more real time and also consumes lesser bandwidth and also a neater way to really do this.


    I made a C# app which works with the comet style method. I don't put the IM in the URL though: I added it as a cookie like that:

    request.CookieContainer = new CookieContainer();
    Cookie cookieIM = new Cookie("IM", sIM, "/", ".messenger.yahooapis.com");
    request.CookieContainer.Add(cookieIM);

    In the example (the PHP one as far as I remember, they put the IM directly in the url so i should work too)
    Hope it can help
    0
  • Setting the cookie in the header resolved the problem, even in the Yahoo SDK documentaion they are not mentioning that we should add the cookie in the cookie headers, and infact in their examples they are themselves showing to put it in the querystring :PHope it can help
    0
  • can you please post the code for receving message on real time? I have send, amd receive message.
    0
  • QUOTE (infoslast @ Feb 23 2011, 02:34 AM) <{POST_SNAPBACK}>
    can you please post the code for receving message on real time? I have send, amd receive message.


    If you are coding in as3 flex then this is how you send IM as cookie

    var getURL:String = "http://"+ notifyServer +"/v1"+ "/pushchannel"+ "/"+ primaryLoginId+
    //getURL = "http://rproxy1.messenger.yahooapis.com" +"/v1"+ "/pushchannel"+ "/"+ primaryLoginId+
    "?"+
    "sid="+sessionId+
    "&seq=0"+
    "&format=json"+
    "&rand=123456"+
    "&idle=30"+
    defaultOAuthParameters();

    httpService.url = getURL;

    httpService.method = "GET";
    httpService.contentType = "application/json;charset=utf-8";

    var urlReq:URLRequest = new URLRequest();
    urlReq.method = URLRequestMethod.POST;
    urlReq.requestHeaders.push(new URLRequestHeader("Cookie","IM="+requestToken));

    httpService.request = urlReq;

    httpService.send();

    IM Value:- IM value is the requestToken you get from login procedure.

    You should also not forget to send notifyServerToken in url parameters in following manner when you create session


    var currentDate:Date = new Date();

    var uuid:String = UIDUtil.getUID(currentDate);

    var url:String = "http://developer.messenger.yahooapis.com/v1/session"+
    "?" +
    "c="+crumb+
    "&notifyServerToken=1"+
    "&realm=yahooapis.com&"+
    "oauth_consumer_key="+consumerkey+
    "&oauth_nonce="+uuid+
    "&oauth_signature_method=PLAINTEXT"+
    "&oauth_timestamp="+new Date().getMilliseconds()/1000+
    "&oauth_token="+oAuthToken+
    "&oauth_version=1.0"+
    "&oauth_signature="+signature+ "%26"+ oAuthTokenSecret;


    var req:Object = new Object();
    req.presenceState = 0;
    req.presenceMessage = "I am not here right now";

    var clientCapabilities:Array = new Array();
    clientCapabilities.push({"clientCapability" : "smiley"});
    clientCapabilities.push({"clientCapability" : "buzz"});


    req.clientCapabilities = clientCapabilities;

    var jsonReq:String = JSON.encode(req);

    httpService.method = "POST";
    httpService.contentType = "application/json;charset=utf-8";
    httpService.url = url;
    httpService.send(jsonReq);
    0
  • Thanks. I am using c#. From login i obtain, sessionID, server, and Notify_Server. Cn you explain again, how can I obtain IM frm sign on function?
    I have also implemented as I said, send, receive message. Does anyone has the code for real time send receive in c#?
    0
  • QUOTE (infoslast @ Feb 23 2011, 08:20 AM) <{POST_SNAPBACK}>
    Thanks. I am using c#. From login i obtain, sessionID, server, and Notify_Server. Cn you explain again, how can I obtain IM frm sign on function?
    I have also implemented as I said, send, receive message. Does anyone has the code for real time send receive in c#?


    IM is nothing but the requestToken you get as part of u r login process

    The call looks like this:
    https://login.yahoo.com/WSLogin/V1/get_auth...key=consumerkey

    The result of this call is a single value, the RequestToken:

    RequestToken=jUO3Qolu3AYGU1KtB9vUbxlnzfIiFRLPdfasdfsdf...

    this is the value I am using for IM
    0
  • QUOTE (asif @ Feb 23 2011, 08:28 AM) <{POST_SNAPBACK}>
    IM is nothing but the requestToken you get as part of u r login process

    The call looks like this:

    https://login.yahoo.com/WSLogin/V1/get_auth...key=consumerkey

    The result of this call is a single value, the RequestToken:

    RequestToken=jUO3Qolu3AYGU1KtB9vUbxlnzfIiFRLPdfasdfsdf...

    this is the value I am using for IM
    0
  • so IM is the request_token value, which is different from oauth_token, no?
    0
  • QUOTE (developer @ Feb 28 2011, 06:42 AM) <{POST_SNAPBACK}>
    so IM is the request_token value, which is different from oauth_token, no?


    Yes, request_token is different from oauth_token
    0
  • at poll notification I use the url like in php program :url = URL_YM_NOTIFICATION_LONG +
    "?oauth_consumer_key=" + CONSUMER_KEY +
    "&oauth_nonce=" + randNo.ToString() +
    "&oauth_signature_method=PLAINTEXT" +
    "&oauth_timestamp=" + DateTime.Now +
    "&oauth_token=" + OAUTH_TOKEN +
    "&oauth_version=1.0" +
    "&oauth_signature=" + SECRET_KEY + "%26" + OAUTH_TOKEN_SECRET +
    "&sid=" + SESSION_ID +
    "&seq=" + System.Convert.ToInt16(seq) +
    "&format=json" +
    "&count=100" +
    "&idle=120" +
    "&rand=" + randNo.ToString() +
    "&IM=" + IM; // the IM I get from the response of: login.yahoo.com/WSLogin/V1/get_auth_token .I use the method 'GET' and the response server returns 403: forbiden. why?
    0
  • Sorry for delay, you should not pass IM as part of URL, IM value should be passed in request, following is an example AS3 code

    var urlReq:URLRequest = new URLRequest();
    urlReq.method = URLRequestMethod.POST;
    urlReq.requestHeaders.push(new URLRequestHeader("Cookie","IM="+requestToken));

    httpService.request = urlReq;

    httpService.send();
    0
  • hi,

    Thank you. How would this be in c# : urlReq.requestHeaders.push(new URLRequestHeader("Cookie","IM="+requestToken));

    Thx for answers!
    0
  • QUOTE (developer @ Mar 9 2011, 08:12 AM) <{POST_SNAPBACK}>
    hi,

    Thank you. How would this be in c# : urlReq.requestHeaders.push(new URLRequestHeader("Cookie","IM="+requestToken));

    Thx for answers!


    Have no idea about C#, you have to find that out. I am stuck with logout issue, can you can help me with it
    Visit My Issue
    0
  • I have a bad request, error 400 at pool notification. Can you please help me?

    string uri = URL_YM_NOTIFICATION_LONG +
    "?oauth_consumer_key=" +consumer_key +
    "&oauth_nonce=" + randNo.ToString() +
    "&oauth_signature_method=PLAINTEXT" +
    "&oauth_timestamp=" + DateTime.Now +
    "&oauth_token=" + oauth_token +
    "&oauth_version=1.0" +
    "&oauth_signature=" + secret_key+ "%26" + OAUTH_TOKEN_SECRET +
    "&sid=" + sessionId +
    "&seq=" + System.Convert.ToInt16(seq) +
    // "&format=json" +
    "&count=100" +
    "&idle=120" +
    "&rand=" + randNo.ToString();
    I use httpwebrequest x ;
    x.method = "get";
    x.ContentType = "application/json;charset=utf-8";

    x.CookieContainer = new CookieContainer();

    // CookieContainer container = new CookieContainer();
    Cookie cookie = new Cookie("IM", IM, "/", ".messenger.yahooapis.com"); IM is get_token from URL_OAUTH_ACCESS_TOKEN
    x.CookieContainer.Add(cookie);
    i get error 400 at httpwebresponse. why?
    0
  • IM looks like this: IM= "otpsa0Zu3AZhFoCZBqEcI3LspJkzumoa9DAFIv9YAWbTrdh.dnr89gWMSNwE4._zpJCrEQ0gcD8
    8hiwjEd21Y9Vr7dYAWed0rKJ5zs_HFN28gQn6mA75Rzp_9mm4wF4tHkpnZ3JxMAfKle3a8W7qhlpa7iva
    PkMt_doQ4jdHfIcrVH0KGi5GzSn6ZYZ_8kSwGoURL.sDvKMTkXZXN9sZoqo-"


    it's ok??
    0
  • I also cannot put x.connection = "keep-alive" because i got argumentexception unhadled: Keep-Alive and Close may not be set using this property.
    Parameter name: value

    how can I resolve error 400. i also get error 403: forbidden at httpwebresponse
    0
  • Have you given notifyServerToken=1 in your url when you were making request for sessionId

    CODE
    var url:String = "http://developer.messenger.yahooapis.com/v1/session"+
    "?" +
    "c="+crumb+
    "notifyServerToken=1"+
    "&realm=yahooapis.com&"+
    "oauth_consumer_key="+consumerkey+
    "&oauth_nonce="+uuid+
    "&oauth_signature_method=PLAINTEXT"+
    "&oauth_timestamp="+new Date().getMilliseconds()/1000+
    "&oauth_token="+oAuthToken+
    "&oauth_version=1.0"+
    "&oauth_signature="+signature+ "%26"+ oAuthTokenSecret;
    0
  • yes, at login proceduce i had that. i modified as you said, but i still get 403 forbidden ( and if i take out from utl format = json or 400 bad request). is the url ok, or I forgot to add something? I use method get, and contentType = "application/json;charset=utf-8"
    0
  • i think it is about the IM. Can you please shoe me your url? To see where in url your IM is located in cookie? or how is added in the url
    thx a lot
    0
  • so I get error 403 , forbidden. Why?

    thx for responses
    0
  • Why are you using count in your URL? there is no need of count, following is the snippet of my url.

    CODE
    var getURL:String = "http://"+ notifyServer +"/v1"+ "/pushchannel"+ "/"+ primaryLoginId+
    "?"+
    "sid="+sessionId+
    "&seq=0"+
    "&format=json"+
    "&rand=123456"+
    "&idle=30"+
    defaultOAuthParameters();
    0
  • sorry pls tell me what defaultOAuthParameters() means?

    thx very much
    0
  • urlNOTIFICATION_LONG +
    "?oauth_consumer_key=" + consumerkey +
    "&oauth_nonce=" + randNo.ToString() +
    "&oauth_signature_method=PLAINTEXT" +
    "&oauth_timestamp=" + DateTime.Now +
    "&oauth_token=" +oauthtoken +
    "&oauth_version=1.0" +
    "&oauth_signature=" + SECRET_KEY + "%26" + OAUTH_TOKEN_SECRET +
    "&sid=" + sessionid +
    "&seq=" + System.Convert.ToInt16(seq) +
    "&format=json" +
    // "&count=100" +
    "&rand="+ randNo.ToString() +
    "&idle=30";
    x.CookieContainer = new CookieContainer();

    // CookieContainer container = new CookieContainer();
    Cookie cookie = new Cookie("IM", IM, "/", ".messenger.yahooapis.com");
    x.CookieContainer.Add(cookie);
    i use method get
    I still have the error 403 forbidden. Pls tell me why?
    0
  • i need to add cookie in the header? how do I do that by having httpwebrequest?
    0
  • Yes, Somehow you need to send IM as cookie in request of httpService. And your url looks alright to me. Can you elaborate on my logout problem please Logout problem
    0
  • still got error 403 forbidden. if someone knows why would be really appreciated if reply.


    thx
    0
  • Has this issue been solved ?? I am looking for something similar for android app.Is there any link tou suggest to get this working ???
    I am pretty new to Jason too ??
    0
  • Recent Posts

    in Messenger IM SDK