Socket.IO Best way to send messages between two users? [closed] - javascript

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 6 years ago.
Improve this question
I want to know what is the best way to send messages between two users? I know you can do rooms and join them but you have to "create" them first. Think of it like chat messenger. You only show the messages you recieve between those two users.
I can do one big object but eventually that would be a big object.
What are your suggestions on handling this?

Every socket in Socket.io has its own ID. You can send messages directly to a socket, once you know that ID. Example from https://socket.io/docs/v3/rooms/index.html#Default-room
io.on('connection', socket => {
socket.on('private message', (anotherSocketId, msg) => {
socket.to(anotherSocketId).emit('private message', socket.id, msg);
});
});

Related

Make new webpage from input [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 years ago.
Improve this question
So, what I would like to implement is something like the twitter new-user function, which makes a new page on their website based on the user's input - such as https://twitter.com/newuser
How would I go about doing this? (I am fluent in NodeJS, Express, Javascript, MongoDB - the node client too, CSS, HTML)
You can define a dynamic route in express and show user data based on url paramter.
app.get('/:userName', function (req, res) {
res.send("this is the " + req.params.userName + " Profile");
})
You shoud make a database table to store users data. (and username will be unique).
after that in your profile route you can get username from url and read user data from database based on userName parameter.

How to handle seen/unseen chat messages in React/Firebase chat app? [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 4 years ago.
Improve this question
I’m trying to handle seen and unseen message status in one-to-one chat app using React and Firebase realtime database.
Can I store for every send message in the database, seen: false value and when the receiver mount the component (message), to make again request to the database and change the value of “seen” to “true”?
Are too many database requests can be an issue here?
Is it a good practice to store for every send message in the database, seen: false value and when the receiver mount the component (message), to make again request to the database and change the value of “seen” to “true”?
I don't think you need to do it for every message.
Let's say you have User1 and User2 who have a private conversation.
User1 sends two messages:
Id: 1 Message: "hello"
Id: 2 Message: "how are you"
User2 then looks at the messages - you then send to the server:
User: 2 ChatID: abc LastViewedMessage: 2
User1 comes back to the messages, and receives from the server User2's lastViewedMessage id.
I don't see a reason to store it on a "message by message" basis - unless you specifically want the functionality to mark only some messages as viewed.
You could also store the timestamps of the messages, and a "last-viewed-time" rather than message ids.

Permanent change to DIV after data has been saved to DB [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 6 years ago.
Improve this question
How do I make this permanent and global?
By this I mean the color change of the div!
The data has been stored to DB and the div must remain red for all times for all computers ha-ha and not until the page reloads! How can this be achieved?
var x = document.getElementById("mySelect1").selectedIndex;
var y = document.getElementById("mySelect2").selectedIndex;
var color = "#ff0000";
request.done(function (data) {
if (data != -1) {
if (x==0 && y==0) {
document.getElementById("A9").style.backgroundColor = color;
}
alert("You Have successfully made an appointment");
location.assign("AjanvarausPage.html");
}
Server-side code will be needed to keep track of the change and notify all clients. The former will require some form of persistence, global memory/cache or database perhaps, while the latter is the trickier part. Since a server generally doesn't know if a web client is connected or not it has to be told there is a client needing to be notified.
But there are a number of ways this could be done.
Polling
Each client must poll the server-side code for potential changes. There are in general two different types of polling, short and long.
Short Polling
each client will need to ask the server if there are changes in a loop, usually with a delay to prevent hammering the server. The server responds immediately notifying the client if there are changes or not.
Long Polling
similar to short polling, with the single exception of the server not responding unless there are changes. This keeps the request open until it is either satisfied (there are changes to report) or it times out.
Push Notification
There are technologies, such as SignalR, that has the client register with the server and can then be notified repeatedly without further notification.

Live Streaming and SSE [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 8 years ago.
Improve this question
I want to work on a project where i want to set a server which hosts videos, pictures, and texts. The main objective is to push up the media contents update to the client using Sever-Side Event.
It is more or less like digital signage player where i have to set up my own server and the client player. I'm totally new at this thing. Can someone give me any ideas on how i can do it. Just basic ideas would be great and i can search about them.
You might want to check out node-easysse and easysse-client
server
var easysse = require("easysse");
app.get("/chat-stream", easysse);
app.post("/chat", function(req, res) {
easysse.emit("chat", req.body.username, req.body.message);
});
client
<script src="easysse-client.js"></script>
<script>
var client = easysseClient.connect("/chat-stream");
client.on("chat", function(username, message){
console.log(username, "says", message);
});
$.post("/chat", {username: "mjackson", message: "hehe"});
// "mjackson says hehe"
</script>
Api docs
node-easysse API
easysse-client API

Server-side validation to prevent illegal POST requests [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 8 years ago.
Improve this question
For my webapp (angular + node.js), I'm implementing a gamification system where users gain points for performing actions like answering questions, watching a video, etc. Currently, I simply post to '/api/users/updatepoints' with the user's id and number of points changed to update the database.
This is of course not secure, since a user could easily do an ajax request from the console while logged in, and I was wondering how I can prevent users from illegally sending out ajax requests? What sort of server-side validation could I use to do so?
//front end
$http.post('/api/users/updatepoints', {
kidId: xxx,
pointsChanged: yyy
})
//backend
exports.updatePoints = function(req, res) {
var kidId = req.body.kidId,
pointsChanged = req.body.pointsChanged;
User.findOne({_id: kidId}, function(err, user) {
if (err) return res.send(400);
user.points += pointsChanged;
user.save(function(err) {
if (err) return res.send(400);
return res.send(200)
});
})
}
The simple answer is "you can't".
A sufficiently determined hacker will always be able to take control of anything you are running on their computer. The only way to avoid that is to validate everything server side.
For example the only way to defeat "map hacks" in competitive online play is to never send information to the client unless that information is being displayed to the user.
If it's important, do it server side. Let the client side do its processing and validate and verify everything it sends you.
This is much too big a subject to properly discuss in a format like this though. Try doing some internet searches on preventing client hacks in games.

Categories