Live Streaming and SSE [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 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

Related

How do I fetch frontend input from with express WITHOUT using <form> [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 7 months ago.
Improve this question
I am randomly wondering how to get information based on input without using the <form method = "POST" ....> using express.js how would I access the req.body of something like this from the without using a form entry on the front end? - just curious
If I understand your question correctly, you are asking how to send data in an HTTP request.
How are you sending your HTTP requests from the front-end - perhaps Axios? That's a detail worth providing in your question.
For an Axios POST method, you can provide a request body as the second parameter, with the first parameter being the URL. See the Axios documentation here: https://axios-http.com/docs/post_example
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
The second parameter is a simple JavaScript object. To populate req.body, use body-parsing middleware such as express.json().
https://expressjs.com/en/4x/api.html#req.body
If you're just using an API platform tool like Postman, then in your request Body you can choose "raw" from the radio button and then JSON from the dropdown.

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.

Socket.IO Best way to send messages between two users? [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
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);
});
});

Using rails how to synchronize variable in controller to the view where the controller is the initiator of the update [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 8 years ago.
Improve this question
I tried the long polling in AJAX, where the client calls the server requesting data at a certain interval. - The updating of list in UI was initiated by the client
Below are the code:
server_controller.rb
##x=0
def results
##x++
render :json => ##x
end
index.html.erb
<button id='get_value' class="btn">Run</button>
<ul id="value_variable_from_controller"><ul>
$(document).ready(function() {
var getPoll=function getValue(trigger) {
$.ajax({
type: 'GET',
url: '/server/results/',
success: function(data, status) {
if (data < 50){
$('#value_variable_from_controller').append("<li>"+data+"</li>");
setTimeout(getValue(true), 1000);}},})}
$(document).on("click","#get_value",getPoll);
The Question is how can i do it oppositely where the controller has the power to initiate/send the data and the list on HTML was synchronously updating/appending?
This is more tricky to do, because you need an additional technology. HTTP and its request-respond model can't do this. A client (browser) can't receive requests from a server.
You'll end up with a bidirectional communication, because first the client ask the server things and later the server asks the client to update. A technology for doing this is websockets. Rails don't include this, so you need to add it via gems (websockets-gem) or/and probably an event driven, non-blocking server like node.js is useful as well.
There are different tutorials out there, which give you a start. Here is one example with two nice little graphics, which explain the situation.

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