3

There is full read and write chat functionality now added to the API :)

Who wants to write two-way IRC<-->DaniWeb Chat integration? ;)

Any takers ... ??

You need an access token in order to post, but it's feasable if you create a new DaniWeb account named "IRC Bot" and have everything posted from the IRC channel posted by the bot (perhaps the message can be preceded by the nick in parentheses or some such).

Votes + Comments
Good work!
9
Contributors
77
Replies
395
Views
4 Years
Discussion Span
Last Post by Reverend Jim
Featured Replies
  • 1
    Dani 1,665   4 Years Ago

    OK, long polling set up!! :) Read More

  • 1
    diafol 3,720   4 Years Ago

    OK, here's my js/jQ and it seems to be using long polling and the since_id also seems to be working: //EDIT - THis doesn't work properly. <div id="chat"></div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> <!-- var firstRun = true; getChat(); function getChat(lp) { var usenum = (!lp) ? 0 : lp; $.ajax({url: … Read More

  • 1
    Dani 1,665   4 Years Ago

    Diafol, I copied and pasted your code *exactly* and simply replaced `client_id = 1` at the beginning, and it works fine. Read More

  • 1
    Dani 1,665   4 Years Ago

    Gah I think I missed you in live chat by about a minute. Let me know if it's fixed now. Read More

  • 1

    I'll vote for a spectacular nosebleed :D Read More

0

Something else I'd love to see done: I'd like to pull in my Twitter tweets and Facebook status messages into my Shoutbox.

0

Chat sounds interesting, hadn't noticed the changes to the API.

So, you wouldn't be too impressed with somebody calling a fetch every few seconds then, to replicate 'a live feed'?

0

That would be fine. Chat was added to the API specifically to offer the ability to create a live chat application, so polling just comes with the territory.

I'll go ahead and see if I can add long polling to the API as well.

0

Nice one Dani. I may have a pop at creating an app with this - although too late for comp. :)

0

Awesome :) Yeah, I think some features with the chat would be much appreciated by all, especially since it has write capability.

0

hadn't noticed the changes to the API.

Some version info would be nice. Just noticed changes to the get chat, would be nice if there was a history of sorts, make it easier to keep track.

0

Yeah, sorry about that. :( I was going to start a new thread mentioning the changes but I held off figuring no one started working with chat yet :(

0

I keep getting error messages wrt timing out. I've only started playing, so it's probably me. :)

0

Are you using the long polling version or the short polling version?

0

long- but not sure if I had it set up correctly. will try again (I trashed the code).

//tried something different - worked !

Edited by diafol

0

OK cool. Yeah, you just need to put it in a while loop where as soon as it sends something back, immediately make another call.

0
var old;
function getChat()
    {
        $.get('danichat.php',function(data){
            var gr = $.parseJSON(data);
                str = '';
                if(old !==  data){
                    old = gr;
                    $.each(gr.data,function(i,v){
                        str += "<strong>" + v.chatter.username + ": </strong>" + v.message + "<br />"; 

                    });
                    $('#chatbox').html(str);
                    getChat();
                }
        });
    }

That's my rough draft. I'll tidy it up with just appending new data by getting the latest post, instead of writing the whole thing every time.

0

Not sure what danichat.php does, but yeah, the JSON will include a data array in addition to a polling array that contains the last ID # retrieved. You'll want to pass that back in ....... on that note, please don't kill me, but I accidentally changed last_id to since_id so I just changed it back to last_id. Don't kill me!! :)

0

please don't kill me, but I accidentally changed last_id to since_id so I just changed it back to last_id

I got dis buddy in Lawn Guyland, who duz de odd job for me. Maybe I pay him to give you a call, huh?

Not sure what danichat.php

That's just the file_get_contents.

0

since_id so I just changed it back to last_id

And you haven't updated the docs...

BTW, long polling states true/false, but your example shows a 1. Even if I poll the forum chat (3) with a high ID, it still returns directly. Shouldn't this wait for 30 seconds?

Edited by pritaeas

0
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="chat"></div>
<div id="clock"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
<!--
var last = false;
var clock = 0;
getChat(1);

    function getChat(lp)
    {
        defaultStart = 10269;
        usenum = (lp == 1) ? defaultStart : lp;
        $.ajax({
            url: 'http://www.daniweb.com/api/forums/3/chat', 
            data: {long_polling: 1, last_id: usenum},
            dataType: 'jsonp'
        }).done(function(data){
            id = data.polling.last_id;
            chatData = data.data;
            if(last == false)
            {
                initializeChat(chatData); 
            }else if(last !== id){
                appendChat(chatData);   
            }
            clockIt();  
            last = id;
            getChat(id);
        });
    }


    function initializeChat(data)
    {
        str = '';
        $.each(data, function(i,v){
            str += '<strong>' + v.chatter.username + ':</strong> ' + v.message + "<br />";  
        });

        $('#chat').html(str);   
    }

    function appendChat(chatData)
    {
        alert('called'); //write append data again      
    }

    function clockIt()
    {
        clock = clock + 1;
        $('#clock').html(clock);
    }

//-->
</script>
</body>
</html>

Just tried this code - works after a fashion, no php being used this time :) ANyway, I thought you were running a long poll (approx 30 seconds?). My code is running extremely quickly. About 90 times / minute. It shouldn't be doing that should it? Seems the api is returning data immediately as opposed to long polling - or have I made an error (again)?

Edited by diafol

0

Seems the api is returning data immediately as opposed to long polling

I've come to the same conclusion in PHP.

Does the polling add "clicking around somewhere" to the online stats?

Edited by pritaeas

0

I accidentally changed last_id to since_id so I just changed it back to last_id

You're confusing. Why not just say "it should be since_id". Works now.

The problem was (since diafol and I must've been using last_id) that the id starts at 0 (when omitted), and the full result is returned every time.

Edited by pritaeas

0

I did ... on the previous page. But I changed it in too many places so one of the places I changed it to had to be changed back to last_id. That's what I meant where you quoted me.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.