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


'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() {
ws.onmessage = function(d) {
  mongoInstance.insertOne(JSON.parse(d.data)) //Promise which add the data

ws.onclose = function() {
  //maybe a reconnect attempt ?

ws.onerror = function(e) {

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 ?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.