Client Side Framework:: (Return HTML - CODE )
Client Side Framework:: (Return HTML - CODE )
The code simply checks if the scope username variable is set when user clicks to join collaborative
draw/chat application, if it is then an alert is shown to welcome the user using his/her username, otherwise
it asks him/her to specify it.
This would be done using React.js components (we will focus on login page only):
setUsername(e)
{
e.preventDefault();
if(this.ref.username.length > 0)
Sample Assignment For Reference Only
this.username = this.refs.login;
}
isAuthenticated()
{
return this.username != null;
}
render()
(
if(this.isAuthenticated()){
return <MainComponent />
}
return
<form onSubmit={ this.setUsername }>
<input placeholder="Username" ref="username" />
<button>Start chat</button>
</form>;
);
}
app.use(express.static('public'));
server.listen(process.env.PORT || 3000);
console.log('Server started at port 3000');
Sample Assignment For Reference Only
Note that each socket.emit is sending to each socket instance from the socket client, and socket.on is
listening for an event (specified as the first parameter) from the socket client.
Sails.js provides a socket client, “The Sails socket client (sails.io.js) is a tiny browser library that is bundled
by default in new Sails apps. It is a lightweight wrapper that sits on top of the Socket.IO client whose
purpose is to make sending and receiving messages from your Sails backend as simple as possible.” -
http://sailsjs.com/documentation/reference/web-sockets/socket-client
Sails.js use the concept of group (room), a group is first created base on a model (nodejs module), then on
each new client connection we need to subscribe them to the group (using watch method from sails socket
client), and publish each of their message to the group (using publishCreate method)
On the socket server (a controller to catch users requests and either subscribe them or publish their
messages depending on the method used to request)
module.exports = {
addConv: function(req, res)
{
var data_from_client = req.params.all();
if (req.isSocket && req.method === 'POST') {
// This is the message from connected client
// So add new conversation
Chat.create(data_from_client).exec(function(error, data_from_client) {
Chat.publishCreate({
id: data_from_client.id,
message: data_from_client.message,
user: data_from_client.user
});
});
} else if (req.isSocket) {
// subscribe client to model changes
Chat.watch(req.socket);
}
}
};
module.exports = {
attributes: {
user:{
type:'string'
},
message:{
type:'string'
}
}
};
Finally the user (socket client) would first subscribe to the group using get request then send
message via post requests:
io.socket.get('/chat/addconv');
sendMsg = function() {
io.socket.post(
Sample Assignment For Reference Only
'/chat/addconv/',
{ user: $scope.chatUser, message: $scope.chatMessage }
);
$scope.chatMessage = "";
};