WebRTC: Transmit message from Node to Browser - node.js

I'd like to know how could I use webRTC to send a message from Node.JS to my Browser without having to install any third-party lib on the Browser end.
I've researched a few options but they all were outdated or over complicated. I just need to print something on the console, no UI, no nothing.
Maybe someone could help me out with a snippet or example?
Thank you

the easiest way is to use socket.io to send node server to browsers without having to install any plugin or lib on browser side(you need to add socket.io.js to your code). onces you setup socket.io you can basicly send data from node server to browser by
client.emit("message",message);
and you can get this from client side like
socket.on("message",function(message){
//do something with message
}};
you can find more information from https://socket.io/docs/ about socket.io
if you would like to send data from browser to browser you need to set up a signaling server to connect the peers. from https://github.com/webrtc/samples link you can find lots of examples for WebRTC implementations for different scen

Related

How use app.use in io.on?

How I can use app.use in socket.io? E.g.
io.on('connection', function(data){
app.use('uri here', function(req, res){
// emitting here
});
});
It's really?
I have:
ss1.example.com (head-server for caching online users with users servers).
ss2.example.com (first app server)
ss3.example.com (second app server)
You're misunderstanding something here. The sequence of events when a web page is loaded is as follows:
User initiates page load (by clicking on something or by typing something in the URL bar or by selecting a bookmark).
Browser parses the server, gets the host and port out of the URL and sends an http GET request for the path to the IP address for that host and port.
Web server receives the GET request and sends back to the browser an HTML page.
The web server may or may not use middleware when that request is received (depending upon what it wants to do).
Browser parses the HTML page and then runs scripts in the page.
Javascript in the web page runs and initiates socket.io connection to some host (often to the same host that the web page came from).
Web server receives socket.io request and socket.io handle recognizes the web request as a socket.io connection request.
Server-side socket.io code responds to client request for socket.io connection and a socket.io connection is initiated between browser and server.
Client or server can then send data over the socket.io connection.
Now, to your question about where to insert app.use(). That is for http middleware. You would insert that in the regular web server request chain, typically right before you app.get() and app.post() request handlers. You would not typically use http middleware for a socket.io connection.
If you wish to run some code before any socket.io connection connects, then you would use io.use() and use a socket.io middleware handler. That will give you access to the http request information on every socket.io connection request.
If you wish to run some code on every socket.io message that is received (regardless of the message name), that is not a supported feature of socket.io. There are some add-ons that hack into socket.io to allow you to essentially do a socket.on('*', ...) type event handler for all incoming socket.io messages, but that is not something that socket.io supports as a built-in feature.
If one of these options still doesn't sound like what you want, then please explain to use what actual problem you're trying to solve and we can better make a suggestion for that particular problem.
Note your question is a bit like an XY problem where you asked how to do what you think is the solution (using app.use() for socket.io) rather than describing the actual problem you want to solve. The problem with that type of question is that if you're wrong about the solution direction, then all we can really tell you is that you're wrong with that solution because you didn't describe the actual problem so we can direct you to the right type of solution. In the future, you will probably get better answers if you make sure to describe the problem you're trying to solve, not just the solution you're trying.

Update Data in one webpage and displayed in other webpage

I need to update data(new offers or any other information) in a webpage. This update must be done by client and update must be displayed in the same webpage.
For eg: webpage called mirror.com and other webpage called mirror.com/update-
here,client will input data in mirror.com/update page and it must be displayed/updated in mirror.com page.
I'm using nodejs
Are you asking for real time updates? Then you have to use a Websocket connection.
You can use socket.io, sockjs or pusher.com for this. In the nodejs community , many people use socket.io. you can easily implement it into your project.
You have to build your own websocket server for socket.io and sockjs, but you don't want to do that when you use pusher. But socket.io, sockjs is much faster than pusher as i know. Because of when you use pusher , you have to connect to pusher server and grab the data back and then again you have to connect your own sever to display the data. But when you use socket.io or sockjs, you are working with the same sever.

socket.io : associate browser and computer with the connection

I am writing a game in node.js with socket.io library. It has a server to whom 2 clients connect and play. My game is working fine, but there is a problem.
If a client that is already connected connects again to the game it is considered as 3rd connection request which messes things up.
This usually happens when I restart my node server when client browsers were not closed. I do I get around this.
You can use cookies. There are modules for this: Socket.IO Authentication
But you can also do it manually, as it is a simple cookie:
When a user connects, add a step to identify the user before starting the game. This step should create a cookie or use the existing one and send it to the server for authentication.
It's not difficult, you can read and write cookies with javascript, and sending a String/number to the server is not a problem either (websocket.emit('auth',whatever)).
Based on this example, it looks like its possible to use cookies to identify sessions in socket.io, but you may be better off using query string values to identify a particular browser or computer:
https://www.exratione.com/2013/05/the-use-of-cookies-versus-query-string-tokens-to-identify-sessions-in-socketio/

Connecting to socket.io from pure js

Hi I looking for a documentation or example to connect to socket.io running on nodejs at server side from a pure JS from a client.
I have to remove the socket.io.js lib from the client side.
socket.io provides various transport layers. If you are happy to experiment and are sure that your client supports WebSocket in all cases, then you might be successful in reproducing the WebSocket-based part of the socket.io protocol on the client side. This, however, seems to be a tedious task compared to just using the code that you would re-invent (partly) anyway.
Use socket.io on the client side, or take an entirely different approach, also on the server side.

Passing streaming of Http response to a web page using node.js

Following up on this question I was wondering is there is a way, without using socket.io, to avoid the buffering of the response that happens on most navigators. So for instance if the node server emit every 5 secondes : 'hello world' i can directly print them on a webpage as soon as the data is available.
Is there a way to do so ?
Unfortunately, this is not how web browsers work. If you want this type of functionality without using WebSockets (or a socket.io fallback) you could try with Server-Sent Events. See this gist for an example (in coffeescript). Also, here is a polyfill for older browsers.
Yes, this is doable. This is how comet streaming servers work.
See http://faye.jcoglan.com/ for an example for Node.js.

Resources