Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added message persistence

  • Loading branch information...
commit 2ced3d496d5fb8d6a82a317550c846f0b926d3e0 1 parent 41e5c43
Pat Patterson authored
Showing with 27 additions and 0 deletions.
  1. +27 −0 server.js
27 server.js
View
@@ -18,11 +18,37 @@ var fu = require("./fu"),
var MESSAGE_BACKLOG = 200,
SESSION_TIMEOUT = 60 * 1000;
+
+var pg = require('pg').native; //native libpq bindings = `var pg = require('pg').native`
+var conString = process.env.DATABASE_URL || "tcp://postgres:1234@localhost/postgres";
+
+var client = new pg.Client(conString);
+client.connect();
+
+client.query("CREATE TABLE chat_messages(nick varchar(50), type varchar(10), text varchar(1024), timestamp bigint)",
+ function(err, result) {
+ if (err) {
+ if (err.code === '42P07') {
+ console.log("chat_messages table already exists");
+ } else {
+ console.log(err);
+ }
+ } else {
+ console.log("created chat_messages table");
+ }
+ });
var channel = new function () {
var messages = [],
callbacks = [];
+
+ var query = client.query("SELECT * FROM chat_messages ORDER BY timestamp LIMIT $1", [MESSAGE_BACKLOG]);
+ query.on('row', function(row) {
+ console.log(row);
+ messages.push(row);
+ });
+
this.appendMessage = function (nick, type, text) {
var m = { nick: nick
, type: type // "msg", "join", "part"
@@ -43,6 +69,7 @@ var channel = new function () {
}
messages.push( m );
+ client.query("INSERT INTO chat_messages(nick, type, text, timestamp) values($1, $2, $3, $4)", [m.nick, m.type, m.text, m.timestamp]);
while (callbacks.length > 0) {
callbacks.shift().callback([m]);
Please sign in to comment.
Something went wrong with that request. Please try again.