patternjavascriptMinor
Realtime chat service
Viewed 0 times
realtimeservicechat
Problem
I am new to Node.js and Socket.io. I implemented a realtime chat service that has a chatroom feature. When the actual product is launched, there will be at least 200 concurrent users with 50 groups of 4 people. I would appreciate any advice on its performance and any recommendations.
Server (remote server)
Client
```
WebSockets Demo
WebSockets Test
Entry
$(function(){
var groupid = 2; //this groupid will change in the actual app. Right now is hardcoded. At tun time, this roupid is passed from php.
var socket = io("http://104.236.19.119:3000");
socket.on('connect',function(){
window.alert("You are connected.");
});
$('#button').on('click',function(){
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg);
$("#entry").val("");
});
var groupname = groupid + "chat";
//got the msg and append to chat window
socket.on( groupnam
Server (remote server)
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/'));
//setup IO
io.on('connection', function(socket){
console.log("A user is connected");
var notice = {'content' : 'Hello World'};
socket.emit("notice", notice);
//listen for reply
socket.on("reply",function(data){
//extract group id, make sure the msg is sent to the particular group
var groupid = data.group;
var groupname = groupid + "chat";
socket.emit(groupname, data);
console.log("got msg");
//save the data into mongo db
});
socket.on('disconnect', function(){
console.log("A user is disconnected");
});
});
//Open server
http.listen(3000, function(){
console.log('listening on *:3000');
});Client
```
WebSockets Demo
WebSockets Test
Entry
$(function(){
var groupid = 2; //this groupid will change in the actual app. Right now is hardcoded. At tun time, this roupid is passed from php.
var socket = io("http://104.236.19.119:3000");
socket.on('connect',function(){
window.alert("You are connected.");
});
$('#button').on('click',function(){
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg);
$("#entry").val("");
});
var groupname = groupid + "chat";
//got the msg and append to chat window
socket.on( groupnam
Solution
Your client JavaScript indentation is very, very messed up. I can hardly understand what's going on.
Some lines are missing an indentation, and some have an extra indentation, or even an extra space (which is extra very messed up, because you are leaving the alignment of a tab/indentation).
Everything inside the very first function should be indented one tab. Everything inside the
Here is your tab code cleaned up:
This code is much, much easier to read.
You have a potential error here: in this function, you attempt to send a signal to the server that you are connected to. However, if the client had not yet connected to the server,
To fix this, create a variable called
Here is what I mean:
Some lines are missing an indentation, and some have an extra indentation, or even an extra space (which is extra very messed up, because you are leaving the alignment of a tab/indentation).
Everything inside the very first function should be indented one tab. Everything inside the
socket.on's second parameter should be indented another tab. Everything inside $(#button).on's first parameter should be indented another tab.Here is your tab code cleaned up:
$(function(){
var groupid = 2; //this groupid will change in the actual app. Right now is hardcoded. At tun time, this roupid is passed from php.
var socket = io("http://104.236.19.119:3000");
socket.on('connect',function(){
window.alert("You are connected.");
});
$('#button').on('click',function(){
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg);
$("#entry").val("");
});
var groupname = groupid + "chat";
//got the msg and append to chat window
socket.on( groupname , function(data){
var new_msg = $("" + data.content + "");
$('#msg').append(new_msg);
});
})This code is much, much easier to read.
$('#button').on('click',function(){
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg); // <--
$("#entry").val("");
});You have a potential error here: in this function, you attempt to send a signal to the server that you are connected to. However, if the client had not yet connected to the server,
socket.emit will fail.To fix this, create a variable called
isConnected and set it to false. Then, when the first socket.on fires you can set this variable to true. That way, you can setup an if statement in the code above so that you don't try to reply to the server without first connecting to the server.Here is what I mean:
var isConnected = false;
socket.on('connect',function(){
window.alert("You are connected.");
isConnected = true;
});
$('#button').on('click',function(){
if(isConnected) {
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg);
$("#entry").val("");
} else {
alert("You are not connected yet.");
}
});Code Snippets
$(function(){
var groupid = 2; //this groupid will change in the actual app. Right now is hardcoded. At tun time, this roupid is passed from php.
var socket = io("http://104.236.19.119:3000");
socket.on('connect',function(){
window.alert("You are connected.");
});
$('#button').on('click',function(){
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg);
$("#entry").val("");
});
var groupname = groupid + "chat";
//got the msg and append to chat window
socket.on( groupname , function(data){
var new_msg = $("<p>" + data.content + "</p>");
$('#msg').append(new_msg);
});
})$('#button').on('click',function(){
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg); // <--
$("#entry").val("");
});var isConnected = false;
socket.on('connect',function(){
window.alert("You are connected.");
isConnected = true;
});
$('#button').on('click',function(){
if(isConnected) {
var entry = $("#entry").val();
var reply_msg = {"content" : entry, "group":groupid};
socket.emit('reply',reply_msg);
$("#entry").val("");
} else {
alert("You are not connected yet.");
}
});Context
StackExchange Code Review Q#93344, answer score: 2
Revisions (0)
No revisions yet.