Chat demo in less than 100 lines

509 views
Skip to first unread message

Jorge Ramirez

unread,
Jul 26, 2014, 3:43:31 AM7/26/14
to androi...@googlegroups.com
Hi! I'm very excited learning to program Android Apps with AndroidScript, I really hope to make fun apps in the future.

For this demo I will show you a completely functional Chat application built in less than 100 lines of code, it uses Firebase as a backend (it has exactly 0 lines of code in Firebase, so it doesn't check for anything, try not to break the database, please?).

Just copy paste and test it in one or more devices with an internet connection ;) It's magic!



//Firebase Chat Demo for AndroidScript by cokeramirez - weapps.cl
//v0.01 26/07/2014

//Load the firebase script

//set a random "username"
name = "User "+ Math.floor(Math.random()*999);



//Called when application is started.
function OnStart()
{
    //Force orientation
    app.SetOrientation( "Portrait" );   
    
    //create a new reference to Firebase
    myDataRef = new Firebase('https://chat-demo-androidscript.firebaseio.com/');
    
    //Everytime there is a new child on Firebase, 
    //we will execute the addChatMessage function.
    var query = myDataRef.limit(10);
    query.on('child_added', function(snapshot) {
        var message = snapshot.val();
        addChatMessage(message.name, message.text, message.time);
    });

//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "FillXY" );

    //Create title text
txtTittle = app.CreateText( "Demo Chat App",1 );
txtTittle.SetTextSize( 30 );
txtTittle.SetTextColor( "black" );
txtTittle.SetBackColor( "#F5A623" );
lay.AddChild( txtTittle );

    //Create a text Input 
    txtInput = app.CreateTextEdit( "" , 0.8 ); 
    txtInput.SetTextSize( 16 ); 
    txtInput.SetMargins(0,0.01,0,0.01);
    lay.AddChild( txtInput ); 

    //Check if the user entered a new line
    txtInput.SetOnChange( checkInput );

//Create the chat area
    txtChatArea = app.CreateText( "This is a real chat app written in less than 100 lines. \n You need an internet conection for this to work.\n\nLoading the messages from server..." , 0.9, 0.8, "Left,Multiline" ); 
    txtChatArea.SetTextSize( 16 ); 
    lay.AddChild( txtChatArea );

//Add layout to app.
app.AddLayout( lay );
}

//this function is executed everytime there is a new message on the server
function addChatMessage(name,message,time){
    var date = new Date(time);
    
    txtChatArea.SetText( date.getHours() + ":" + date.getMinutes() + " " + name + ": " + message + "\n" + txtChatArea.GetText());
}

//checks for a new line and submit the text
function checkInput(){
    //app.Alert(txtInput.GetLineCount());
    if (txtInput.GetLineCount() > 1){
        submitChatMessage();
    }
}

//function that sends the message to the server
function submitChatMessage(){
    myDataRef.push({name: name, text: txtInput.GetText(), time: Firebase.ServerValue.TIMESTAMP });
    txtInput.SetText('');
}



Dave Smart

unread,
Jul 27, 2014, 4:11:42 AM7/27/14
to androi...@googlegroups.com
Nice demo.  I've just been chatting to my friend with it...  It does seem like magic!

AxReal GS

unread,
Jul 28, 2016, 9:47:09 AM7/28/16
to DroidScript
Hi! How i can make own firebase? I make and past it to code but its not work!

Netpower8

unread,
Jul 28, 2016, 12:45:52 PM7/28/16
to DroidScript
This is interesting and it works. Need some improvement but the good part it works. Was able to chat with someone. Just so happen some one is also testing this app harhar

JustAnotherDude

unread,
Jul 28, 2016, 1:50:28 PM7/28/16
to DroidScript
very nice :)

i have testet it out and it works very good xd

nikhil baby

unread,
Jul 29, 2016, 9:50:35 PM7/29/16
to DroidScript
How to set a account in firebase for using my database?
Can anybody tell me step by step???? Pls

Netpower8

unread,
Aug 2, 2016, 2:00:26 AM8/2/16
to DroidScript
Lets have a MASSIVE TEST. Use this app today at firebase server time 20:00. I dont know what is your local time. But firebase server time is about 13:55 right now. Come back on 20:00 to have a test. If enough people join in we can chat for a few minutes using this DS app

Netpower8

unread,
Aug 2, 2016, 7:55:36 AM8/2/16
to DroidScript
looks like the server time is local time not exactly server time. so it will display different time for different people. sorry guys.

Netpower8

unread,
Aug 2, 2016, 7:55:37 AM8/2/16
to DroidScript

Dev Opex

unread,
Oct 12, 2016, 11:09:47 PM10/12/16
to DroidScript
Do you mind if I use some of this code I'm new to firebase and stuff like it I have my own firebase made so may I use some of  it

Netpower8

unread,
Oct 13, 2016, 1:24:59 AM10/13/16
to DroidScript
I think its ok for you to use the code. If you have improvements it nice that you can share it with us (the ds community)

Dev Opex

unread,
Oct 13, 2016, 12:16:27 PM10/13/16
to DroidScript
one thing I made me a new firebase and linked it to here but it wont work

Netpower8

unread,
Oct 13, 2016, 12:35:49 PM10/13/16
to DroidScript
does the original sample work ? are you able to see the messages from others (previously made). if yes, then there must be something wrong when you added you code. or maybe your firebase link

Dev Opex

unread,
Oct 13, 2016, 12:38:51 PM10/13/16
to DroidScript
well google recently bought firebase so maybe my firebase isn't compatable with the old firebase script I linked it correctly I just copied all the code to test first and then put my link instead of his/hers

Netpower8

unread,
Oct 13, 2016, 12:46:59 PM10/13/16
to DroidScript
whats the error message ? if any ?...

Dev Opex

unread,
Oct 13, 2016, 3:45:14 PM10/13/16
to DroidScript
none I just put the link and it runs but nothing happens when I send

Netpower8

unread,
Oct 13, 2016, 4:47:20 PM10/13/16
to DroidScript
I dont want to say anything bad. I just tried the chat app just now. On the original code it works. I used my own firebase acct. It also works. So i can safely say this chat app still works.

Dev Opex

unread,
Oct 13, 2016, 7:41:39 PM10/13/16
to DroidScript
I fixed it heres my problem:

mine had the key thing bad4a


I took it out and it worked

Dev Opex

unread,
Oct 14, 2016, 10:47:45 AM10/14/16
to DroidScript
but my base won't reload all the messages they get wiped

Dev Opex

unread,
Oct 14, 2016, 11:12:33 AM10/14/16
to DroidScript
I fixed it my I fixed it yayyyyyyyyyyyyyyyyyy

Dev Opex

unread,
Oct 14, 2016, 11:39:21 AM10/14/16
to DroidScript
nvm its wont work for my real app tho only for my test app

ansari_abdullah

unread,
Aug 17, 2017, 8:09:54 AM8/17/17
to DroidScript
Hey, can anyone teach me to retrieve information from firebase, I have created account but wouldn't understand how to retrieve data.

Angel Martinez

unread,
Aug 23, 2017, 3:52:01 PM8/23/17
to DroidScript
Jorge sabes como usar firebase con droidscript? Podrías ayudarme?

Netpower8

unread,
May 14, 2018, 12:08:02 AM5/14/18
to DroidScript
this does not seem to work anymore. can this work still on the new firebase (after google bought and change firebase sdk)

Ev. rodrigo dos santos

unread,
Jul 29, 2018, 7:49:47 PM7/29/18
to DroidScript
yes, changing if you have create database and put the address in place of myDataRef = new Firebase ('https://chat-demo-androidscript.firebaseio.com/') ;, and change the rules to true true

Ángel Alonso

unread,
Feb 8, 2021, 7:39:21 PM2/8/21
to DroidScript
did you enable read and write in firebase?

Steve Garman

unread,
Feb 9, 2021, 10:37:42 AM2/9/21
to DroidScript
The less than 100 lines of code were written in 2014 when firebase was very different from what it is today.

Whilst it is no doubt possible to write a Chat demo using modern firebase, I suggest it would be easier to start from scratch after studying firebase rather than try to amend the code in this ancient thread.

Reply all
Reply to author
Forward
0 new messages