Client code for interacting with GabberTalk
The code currently provides a base client. gabber-client.js and also a knockout client.
First head over to GabberTalk and sign up for an account
The gabber-client.js file provides a base object for communicating with the GabberTalk service. It's setup to call a gabberTalkBoot()
function. Declare this function in your page, and then require the gabber-client.js file.
The gabbertalk-client requires the JSON constant to be present. Use json2.js if it's not natively avaialble.
The ko client requires the knockout library
This simple setup will get you connected
<script> function gabberTalkBoot(){ GabberTalk.connect({accountId: GabberEnv.accountId, roomName: GabberEnv.roomName}); } </script> <script src='/javascripts/gabber-client.js' async></script>
When configuring GabberTalk, create a global GabberEnv object and put configuration in there. You'll need to do this to configure your account id, and optionally the room you want to use.
GabberTalk, the ko client and in future other clients will use the GabberEnv object to configure.
GabberEnv = {
accountId: 'myAccountId',
roomName: 'myRoomName',
activateLinks: true // Used in the ko client to activate urls in messages
}
GabberTalk was primarily designed for chat. To send a message:
GabberTalk.persistentMessage("Hi There");
To receive these messages:
GabberTalk.on('message', function(msg){ doMessageStuff(msg) });
Each message is tagged with it's sender in the from field. There is at least a sessionId attribute in the from. There can also be username and gravatar and origin (the origin domain the mesage came from)
Gabbertalk allows you to claim nicknames with emails. It does no checking on these however and duplicates are allowed on the server.
GabberTalk.claimNick('homer', 'homer@simpson.com');
This results in a nickChange message coming from that session. To subscribe to nickChange events
GabberTalk.on('nickChange', function(msg){ alert(msg.from.username) });
There can be as many subscriptions as you like
Ask for a list of currently connected clients
GabberTalk.clientList();
Results in a clientList message.
GabberTalk.on('clientList', function(msg){msg.clients typof Array });
The only kind of message that is persisted is a message type of messge. It's possible and useful to send any other kind of message however.
Assume someone on the page has uploaded a file via ajax to some file list on the page.
GabberTalk.sendMessage('refreshSection', {route: '#!/fileList'});
Subscribe to refresh messages:
GabberTalk.on('refreshSection', function(msg){
app.trigger(msg.route)
});
All connected clients would now receive the instruction to refresh the #!/fileList route on their pages.
GabberTalk.recentMessages()
GabberTalk.on('recentMessages', function(recents){ stuff(recents) });
As messages are sent / received, you can inspect, modify and even halt them from progressing. This is done by adding filters. Filters are run before the subscriptions are called and are run in the order they were declared.
Return false from the function to halt the message.
Set a send filter to strip bad words:
GabberTalk.sendFilter('message', function(txt){
if(containsBadWords(txt)){
return false; // prevent the message from going any further. Subscriptions will not be called.
}
});
Set a receive filter to tag mine on a logged in users username:
GabberTalk.receiveFilter('message', function(msg){
if(msg.from && msg.from.username == currentLoggedInUsername){
msg.mine = true;
}
});
There's also a whole bunch of events you can subscribe to so you can be awesome
- load
- connect
- disconnect
- couldNotConnect
- couldNotLoad