patternjavascriptMinor
Node.js chat client
Viewed 0 times
nodeclientchat
Problem
This is a chat using Node.js, socket.io and MongoDB for storage.
I'd appreciate any feedback on what can be improved. I understand that this allows for non-unique usernames to be used, and it's currently a JavaScript prompt, however this is simply a demonstration for entering a username to test the functionality.
index.html
server.js
main.css (not as important here)
```
body,
textarea {
font: 13px "Trebuchet MS", sans-serif;
}
.chat {
max-width:300px;
}
.chat-messages,
.chat textarea {
border:1px solid #bbb;
}
.chat-messages {
width:100%;
height:300px;
overflow-y:scroll;
paddin
I'd appreciate any feedback on what can be improved. I understand that this allows for non-unique usernames to be used, and it's currently a JavaScript prompt, however this is simply a demonstration for entering a username to test the functionality.
index.html
Chat
(function() {
var socket = io.connect('http://127.0.0.1:8080'),
name = prompt('Enter your name');
// Listen for output from server
socket.on('output', function(data) {
var messages = document.querySelector('.chat .chat-messages');
if(data.length) {
// Loop results
for(var x = 0; x
server.js
var mongo = require('mongodb').MongoClient,
client = require('socket.io').listen(8080).sockets;
client.on('connection', function(socket) {
// Connect to MongoDB
mongo.connect('mongodb://127.0.0.1/chat', function(err, db) {
if(err) throw err;
var col = db.collection('messages');
// Emit all messages
col.find().limit(100).sort({_id: 1}).toArray(function(err, res) {
if(err) throw err;
socket.emit('output', res);
});
// Wait for input
socket.on('input', function(data) {
col.insert({name: data.name, message: data.message}, function(err, docs) {
// Emit latest message to all clients
client.emit('output', [data]);
});
});
});
});main.css (not as important here)
```
body,
textarea {
font: 13px "Trebuchet MS", sans-serif;
}
.chat {
max-width:300px;
}
.chat-messages,
.chat textarea {
border:1px solid #bbb;
}
.chat-messages {
width:100%;
height:300px;
overflow-y:scroll;
paddin
Solution
You should only open one connection to mongodb. Otherwise if you have 10 users connected on your chat, you'll have 10 opened connections to mongodb.
From there, I would split the code into small functions. One could be call
So that you can call it like this
var mongo = require('mongodb').MongoClient;
var sio = require('socket.io');
mongo.connect('mongodb://127.0.0.1/chat', function(err, db) {
if(err) throw err;
var messages = db.collection('messages');
client = sio.listen(8080).sockets;
client.on('connection', function(socket) {
messages.find().limit(100).sort({_id: 1}).toArray(function(err, res) {
if(err) throw err;
socket.emit('output', res);
});
socket.on('input', function(data) {
messages.insert({name: data.name, message: data.message}, function(err, docs) {
client.emit('output', [data]);
});
});
});
});From there, I would split the code into small functions. One could be call
onInputMessage and looks something like.var onInputMessage = function(data) {
message.insert({name: data.name, message: data.message}, function(err, docs) {
client.emit('output', [data]);
};So that you can call it like this
socket.on('input', onInputMessage);Code Snippets
var mongo = require('mongodb').MongoClient;
var sio = require('socket.io');
mongo.connect('mongodb://127.0.0.1/chat', function(err, db) {
if(err) throw err;
var messages = db.collection('messages');
client = sio.listen(8080).sockets;
client.on('connection', function(socket) {
messages.find().limit(100).sort({_id: 1}).toArray(function(err, res) {
if(err) throw err;
socket.emit('output', res);
});
socket.on('input', function(data) {
messages.insert({name: data.name, message: data.message}, function(err, docs) {
client.emit('output', [data]);
});
});
});
});var onInputMessage = function(data) {
message.insert({name: data.name, message: data.message}, function(err, docs) {
client.emit('output', [data]);
};Context
StackExchange Code Review Q#40050, answer score: 3
Revisions (0)
No revisions yet.