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).

commented: Good work! +0

Perhaps this will inspire some to participate in the contest.

Either that or you win by default me thinks!

That wasn't the plan. You're up either late, or very early ;)

Up late ... nearly 5 am, geez.

That's when I get up, and am in the office for 5.30am most mornings...

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

Member Avatar

diafol

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'?

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.

OK, long polling set up!! :)

Member Avatar

diafol

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

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

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.

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 :(

Member Avatar

diafol

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

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

Member Avatar

diafol

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

//tried something different - worked !

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.

Member Avatar

diafol

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.

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!! :)

since_id so I just changed it back to last_id

Aargh... just changed that...

Member Avatar

diafol

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.

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?

Member Avatar

diafol

<!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)?

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?

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.

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.