2
\$\begingroup\$

I'm learning websockets and wanted to make a websocket onmessage logger which writes the received data in a mongodb.

I'm starting my script with

node listner.js

listner.js:

'use strict';

let DBAbstract = require('./db-controller');
const WebSocket = require('ws');

// getting an instance of a mongodb connection
let mongoInstance = new DBAbstract();

const ws = new WebSocket('ws://ws-url');

ws.onopen = function() {
  console.log('Open')
};
ws.onmessage = function(d) {
  console.log(d.data)
  mongoInstance.insertOne(JSON.parse(d.data)) //Promise which add the data
};

ws.onclose = function() {
  console.log('Close')
  //maybe a reconnect attempt ?
};

ws.onerror = function(e) {
  console.log(e.code)
};

I made this script so far and it works.

When there is an onmessage Event I'm getting a small JSON like this.

{ "event":2,
  "value": 12,
  "item": 'Spoon' }

I was just wondering if this is might be enough in terms of scalability of the received onmessage Events.

I mean there is no problem when I receive three times of small-JSON's in 10 seconds. What will happen when I'm receiving 1000 small-JSON's in 10 seconds ?

Can I improve this code ?

\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.