Last active
October 29, 2018 18:22
-
-
Save aditodkar/389cce1bb00113fa96b20b5df0e683a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import './chat.css' | |
import io from "socket.io-client"; | |
export default class Chat extends Component { | |
constructor(props){ | |
super(props); | |
this.state = { | |
message: '', | |
date: '', | |
messages: [] | |
}; | |
this.sendMessage = this.sendMessage.bind(this); | |
this.addMessage = this.addMessage.bind(this); | |
this.socket = io('localhost:5000'); | |
} | |
componentDidMount() { | |
this.socket.on('RECEIVE_MESSAGE', data => { | |
this.addMessage(data); | |
//this.setState({ messages: [...this.state.messages, data] }) | |
}); | |
} | |
sendMessage(event) { | |
event.preventDefault(); | |
if(this.state.message !== ''){ | |
this.socket.emit('SEND_MESSAGE', { | |
message: this.state.message, | |
date: Date.now() | |
}); | |
//this.addMessage(this.state.message); | |
//this.setState({ message: '', date: '' }); | |
} | |
}; | |
addMessage(data) { | |
//console.log(data); | |
this.setState({ | |
messages: [...this.state.messages, ...data], | |
message: '', | |
date: '' | |
}); | |
//console.log(this.state.message); | |
console.log(this.state.messages); | |
}; | |
render() { | |
return ( | |
<div> | |
<h2>Hello {this.props.match.params.user}</h2> | |
<div id="chat"> | |
<div className="card"> | |
<div id="messages" className="card-block"> | |
{this.state.messages.map((message, index) => { | |
return ( | |
<div key={index} className="msgBox"><p className="msgText">{message.message}</p></div> | |
) | |
})} | |
</div> | |
</div> | |
<div className="row"> | |
<div className="column"> | |
<input id="inputmsg" type="text" placeholder="Enter Message...." | |
value={this.state.message} onChange={ev => this.setState({message: ev.target.value})}/> | |
</div> | |
<div className="column2"> | |
<button id="send" className="button" onClick={this.sendMessage}>Send</button> | |
</div> | |
<div className="upload"> | |
<form action="/action_page.php"> | |
<input type="file" name="pic" accept="image/*"/> | |
<input type="submit"/> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
const Schema = mongoose.Schema; | |
// Create Schema | |
const MessageSchema = new Schema({ | |
message: { | |
type: String, | |
required: true | |
}, | |
date: { | |
type: Date, | |
default: Date.now | |
} | |
}); | |
module.exports = mongoose.model('Message', MessageSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const mongoose = require('mongoose'); | |
const socket = require('socket.io'); | |
const path = require('path'); | |
const message = require('./model/message'); | |
const app = express(); | |
const mongoURI = require('./config/keys').mongoURI; | |
mongoose.connect(mongoURI, {useNewUrlParser: true}, function (err,res) { | |
if(err){ | |
console.log("There is error: " + err); | |
}else{ | |
console.log("Connected to mongo database") | |
} | |
}) | |
let users = {}; | |
let name = ''; | |
// app.get('/', function (req, res) { | |
// }) | |
// app.get('/:name', function(req, res){ | |
// name = req.params.name; | |
// res.sendFile(path.join(__dirname, "client/public/index.html")); | |
// res.send("Hello world" + name); | |
// }) | |
let db = mongoose.connection; | |
db.once('open', function() { | |
console.log("Database is now connected"); | |
let io = socket(server); | |
io.on("connection", function(socket){ | |
console.log("Socket Connection Established with ID :"+ socket.id) | |
users[socket.id] = name; | |
socket.on('disconnect', function(){ | |
console.log('User Disconnected'); | |
}); | |
let chat = db.collection('chat'); | |
socket.on('SEND_MESSAGE', function(data){ | |
let message = data.message; | |
let date = data.date; | |
// Check for name and message | |
if(message !== '' || date !== ''){ | |
// Insert message | |
chat.insert({message: message, date:date}, function(){ | |
io.emit('RECEIVE_MESSAGE', [data]); | |
}); | |
} | |
}); | |
chat.find().sort({_id:1}).toArray(function(err, res){ | |
if(err){ | |
throw err; | |
} | |
// Emit the messages | |
io.emit('RECEIVE_MESSAGE', res); | |
}); | |
}) | |
}); | |
const port = 5000; | |
let server = app.listen(5000, function(){ | |
console.log('server is running on port 5000') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment