How to parse websocket message received? [closed] - node.js

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
I'm connecting to a websocket server using the "ws" library from node.
The message that I'm receiving is like this:
42["recorded",{"_id":"63ecc4906d46420014bde541","fightId":1,"round":2,"roundTime":65,"name":"knockdown","authorId":123,"targetId":456,"stance":"fence","createdAt":"2023-02-15T11:40:00.085Z","updatedAt":"2023-02-15T11:40:00.085Z","__v":0}]
I want to decode this message to get a JSON or js object with the so that I can run some logic on it.
Also how can I keep the connection alive for my client? Can anyone suggest any good libraries or any way we can do it in "ws" library itself.
I tried JSON.stringify and JSON.parse and tried various threads online but couldn't find any solution.
I want to decode the message to prefferrably something like this:
{ "code":42, "title":"recorded", "message":{"_id":"63ecc4906d46420014bde541","fightId":1,"round":2,"roundTime":65,"name":"knockdown","authorId":123,"targetId":456,"stance":"fence","createdAt":"2023-02-15T11:40:00.085Z","updatedAt":"2023-02-15T11:40:00.085Z","__v":0} }

Related

Performance issue while using node.js, socket.io and MongoDB for chat application like Teams [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
We are using node.js, socket.io, express and Mongo database to build an chat application like Teams.
When user text some message then
Data need to be saved
Respective users will be notified over app if they were actively connected.
Else we need to send firebase notification (It's to notify mobiles)
We are facing performance issue when different users of a group texts messages in a group at a time.Here
we are iterating over list of recipients to store data against each as we are also tracking later on to
know when it is delivered and read by that recipient. We are notifying over socket or firebase.
Due to above computation, performance is degrading and entire app get struck at server side till it finishes.
Note: While saving we are using async function. To return saved reference id (which is auto generated for
each entry) we are using Promise.
I am trying to use worker threads to save and notify but could not able to change code
to use worker thread due to code complexity. Kindly suggest any proper workaround to fix it.

How to perform bi-directional communication between client and server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I want to take three inputs on the frontend- start_val, end_val, and increment_val and post these values to the backend in real time. On the server I want to perform some operation and send it back to client.
I'm confused on how to proceed.
Example Input - start_val = 10, end_val = 50, increment_val = 10( This s taken from the client side .
Output = [10, 20, 30, 40, 50] returned back to the client from the server
To create a Real-Time Web App(Bidirectional communication), you can use Socket.io in your Node.js server
Basically create a Node.js App(with Socket.io and Express.js) and consume socket endpoints with your Client App(Angular7)
This article explains it in detail that how to create a Real-Time App with Node.js, Socket.IO and Angular7

How can I keep a Heroku Node.js instance running indefinitely? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I've written a simple Node server, deployed on Heroku, that connects to a Firebase DB written to by users of an app of mine. The server monitors child changes, sending me a message via Twilio when there's new data I need to review. There's no front end, nor does there need to be. How can I have Heroku, which ordinarily terminates idle dynos, run this indefinitely so that I can have it monitor Firebase and pass on alerts?
Heroku free web dynos that receive no traffic in a 30 minute period will sleep. That prevents the Firebase node.js child_changed listeners from staying active and thus you won't receive the Twilio messages. To prevent that from happening, you can include a very simple setInterval function (like the example in this article) to run at some frequency less than 30 minutes.
var http = require("http");
setInterval(function() {
http.get("http://<your app name>.herokuapp.com");
}, 300000); // every 5 minutes (300000)
So for a long-term strategy I don't see anything wrong with it.
In case something causes your node.js server to crash, you can also include code to notify yourself something needs to be checked:
process.on('uncaughtException', function (err) {
console.log(err);
twilioClient.messages.create({...}, function(twilioErr, message) {
if (twilioErr) {
console.log("twilio error: " + twilioErr);
}
});
}

Which side front-end or back-end , attachment file size check should be placed ? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Suppose client wants to upload image file or any other attachment .
And maximum size can be 10 Mb ,
So where file size check should be placed front-end or server back-end side?
which is better design and why ?
It should be at both ends.
Why at client side(front end).
It will be annoying for user if you throw an error after 10mb data is uploaded and this 10mb data will be also uploaded at server side so you will be wasting servers processing power.
Why at server side(back end)
Some people can hack client side code and upload files more than 10mb so you should have validation at server side also
If the client is browser based, then you must have server side validation because it is easy and widely known how to circumvent a client side validation. You may have a client side validation as well for the normal users.
If the flient is a proper application and it is the only channel to your server, then have a client side valudation becsuse it is closer to your users. If there are multiple channels to the server, then you may want to add the server side validation to enforce consistency accross all channels.

best way to save chat messages in mongodb? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is there a better way to log a chat other than writing to the db every time a message is received by the server. I'm considering saving the log after every chat session rather than per msg.
npm install faye
var client = require("faye") ;
client.subscribe("/myChannel", function(messageRecievedOnChannel){
// each time a message is received in channel,
// log it + save to dB if needed.
} ;
// read Faye manual for deeper understanding.
// https://www.npmjs.com/package/faye

Resources