Permalink
Browse files

Simple chat example

  • Loading branch information...
1 parent 18fc208 commit ed559fde8c2c42b834fbac94f7f690a46a4c9cdd @cgbystrom committed Feb 20, 2011
Showing with 73 additions and 0 deletions.
  1. +42 −0 examples/chat.ejs
  2. +31 −0 examples/chat.js
View
@@ -0,0 +1,42 @@
+<html>
+<head>
+ <title>Chat using Beaconpush</title>
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
+</head>
+<body>
+ <style type="text/css">
+ * { font-family: Arial, Helvetica, sans-serif; }
+ input[type="text"] { font-size: 14px; border: 1px solid #777; padding: 5px; margin: 4px 0 8px 0; }
+ #messages { display: block; width: 500px; height: 300px; border: 1px solid #777; margin: 0; }
+ #message { width: 450px; }
+ #send { font-size: 24px; }
+ ul { margin-left: 0; padding: 4px; list-style-type: none; }
+ </style>
+
+ <h1>Chat using Beaconpush</h1>
+ Name <input id="name" type="text" value="Julia">
+ <ul id="messages">
+ <li>Welcome to this simple chat.<br>Enter your name above and send a chat message below.</li>
+ </ul>
+ <input id="message" type="text" placeholder="Enter a chat message">
+ <input id="send" type="button" value="Send">
+
+ <script type="text/javascript" src="http://beaconpush.com/1/client.js"></script>
+ <script type="text/javascript">
+ $(document).ready(function () {
+
+ Beacon.connect('<%= apiKey %>', ['chat'], {log: true});
+ Beacon.listen(function (message) {
+ $('#messages').append('<li>' + message.name + ': ' + message.text + '</li>');
+ });
+
+ var sendMessage = function () {
+ $.post('/messages', {name: $('#name').val(), text: $('#message').val()}, function (data) {}, 'json');
+ $('#message').val('');
+ };
+ $('#message').keyup(function (e) { if (e.keyCode == 13) sendMessage(); })
+ $('#send').click(sendMessage);
+ });
+ </script>
+</body>
+</html>
View
@@ -0,0 +1,31 @@
+var express = require('express');
+var beaconpush = require('beaconpush');
+var app = express.createServer();
+
+var apiKey = null; // Replace with your key from your account page on Beaconpush
+var secretKey = null; // Replace with your key from your account page on Beaconpush
+if (!apiKey || !secretKey) {
+ console.log('Please set your API and secret key. See chat.js');
+ process.exit(1);
+}
+
+beaconpush = new beaconpush.Client(apiKey, secretKey);
+
+app.set('view options', {layout: false});
+app.set('views', __dirname);
+app.use(express.bodyDecoder());
+
+app.get('/', function(req, res){
+ res.render('chat.ejs', {
+ locals: {apiKey: apiKey}
+ });
+});
+
+app.post('/messages', function(req, res) {
+ beaconpush.channel('chat').send(req.body, function (err, numDelivered) {
+ res.send({result: 'sent'});
+ });
+});
+
+app.listen(3000);
+

0 comments on commit ed559fd

Please sign in to comment.