Babel is an open source chat widget and API built with the PubNub Global Realtime Network. With Babel you can exchange 1024-bit RSA Public Keys, and send Encrypted, Self-Destructing Messages.
###Check out the annotated source.
Babel uses both PubNub and Cryptico. So first, let's include the all the necessary libraries. We can then instantiate a Babel object by calling Babel
with a username string.
<script src="http://cdn.pubnub.com/pubnub.min.js"></script>
<!-- Download both from https://github.com/lw7360/babel -->
<script src="./cryptico.js"></script>
<script src="./babel.js"></script>
<script>
var doge = new Babel('doge'); // Initialize Babel with a username
</script>
listUsers()
returns an object with all currently connected users' usernames and their public keys.
console.log(doge.listUsers());
// {username: publicKey}
// {doge: "olpbSlUEca0VVVqcs9ciUZyP...NNIkqx9lVtUZDYLUWhnjaNrEK4E="}
myKey()
returns your Cryptico RSA key.
sendMessage(recipient, message, ttl)
encrypts message
with recipient
's public key, and sends the encrypted message to recipient
. The message will be accessible through the returnMessages
method for both you and the recipient
for ttl
seconds.
doge.sendMessage('shibe', 'Very greetings shibe. Much Excited!', 5);
// This will send a message to 'shibe', with the message
// 'Very greetings shibe. Much Excited!'.
// Both you and `shibe` will only be able to view this message for 5 seconds.
returnMessages()
returns all the messages you've sent or received that have yet to time out.
console.log(doge.returnMessages());
// {
// shibe: [
// {
// TTL: 5,
// msgID: "2fa4abf3-08d4-4e40-873f-f35540590d04",
// plaintext: "Very greetings shibe. Much Excited!",
// recipient: "shibe",
// sender: "doge"
// }
// ]
// }
onMessage(callback)
let's you set a function to be called whenever you receive or send a message.
var messageHandler = function(msg) {console.log(msg);};
doge.onMessage(messageHandler);
// 'shibe' responds to our earlier message and sends us a message of his own.
// messageHandler would be called and would print the following.
// {
// msgID: "d8fd0f29-81cf-4c35-8aef-59b2c664628f",
// plaintext: "Much hello doge! Very cannot wait.",
// TTL: 5,
// sender: "shibe",
// recipient: "doge"
// }
// msgID is a unique string that can be used to identify the message.
onPresence(callback)
does the same thing for presence events, i.e. users joining/leaving/timing out of the channel.
quit()
causes you to leave the channel. Other users will no longer be able to retrieve your public key or send messages to you.