简体   繁体   中英

How to handle live messaging in Mean stack application?

I'm planning to develop a MEAN Stack application. After searching internet i'm not getting myself so clear on how to handle live messaging in my application which would just work like whatsapp/fb messenger or any other chat applications.

Initially i thought of using setTimeout function and make a REST call for latest data on some specified no.of seconds, but i don't know whether it's a feasible option.

Can anyone help me to understand what would be the best practice to update messages on both server and client sides?

You can use Socket.io. It is a websocket which allows you to communicate between browser and server. It's very easy to setup and start using.

You can install it with npm install --save socket.io (assuming you installed express already 'cause you want a MEAN app)

Then you can start with basics like this:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
   res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
   console.log('a user connected');
});

http.listen(3000, function(){
   console.log('listening on *:3000');
});

You can find the documentations in here Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM