Sunday 22 September 2019

Correct way of creating a realtime application with Cassandra

Right now I have a ec2 instance running Cassandra and a simple websocket server. Is there anything I am missing and I would like to know if this is the correct way to make a "real time" chat application?Client connects to websocket, inserts a message, the message is stored into database, and the message is then sent to users if the record to the database is successful.const cassandra = require('cassandra-driver'); const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], localDataCenter: 'datacenter1' }); const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 3000 }); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { //Insert message into Cassandra DB client.connect() .then(function () { return client.execute('SELECT * FROM test_keyspace.users'); }) .then(function (result) { const row = result.rows; console.log('Obtained row: ', row); response.status(200).json(result.rows); //Send message to other users if record in db is successful }) .catch(function (err) { console.error('There was an error when connecting', err); return client.shutdown().then(() => { throw err; }); }); }); //Then send messages to users connected to the websocket in chatroom ws.on('close', function(){ console.log("I lost a client"); }); });

Submitted September 22, 2019 at 07:51PM by sscarcano

No comments:

Post a Comment