real-time socket.io client web browser - html

I'm trying to create a simple local web-server that collects real time data from a remote server. The remote server is not under my control and it streams out text lines continuosly.
I think scocket.io is Ok for that purpose but I'm really new to node.js and I don't know where is the problem. I've installed socket.io with:
npm install socket.io
then I wrote a simple index.html page deployed in my www dir:
<!DOCTYPE html>
<html>
<head>
<script src="socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://192.168.1.100:8888');
socket.on('data', function(msg) {
var val = msg;
$('#data_value').html(val);
})
</script>
</head>
<body>
<h1>Data Stream</h1>
lines: <span id="data_value"></span><br/>
</body>
</html>
When I load index.html with the browser I can see the web page with the correct title and the the string "lines:" but no data is printed out.
If I try to directly browse the remote server at http://92.168.1.100:8888 I can see the stream.
Can you help me?

92.168.1.*, is the home ip address for routers. You say that the server isn't under your control but it's still being run from the same router your client is trying to connect to.
So you're listening for a call from a server, on port 8888 but this server is a local one. If someone else is running this server you're going to need to ask them to portforward it and send them their public ip address, otherwise your website is just waiting for your PC to be hosting this server and send you the data :(

I think you have misunderstood how socket.io works.
The problem seems to be in socket.on part of your code. Socket.io only responds to socket.io broadcast events not a stream of strings that is being updated by a server.
Is 192.168.1.100 server a socket.io server? If not then your web app will not work as a socket.io client.

Related

How to deploy react app with 2 different ports

I am trying to serve a build on my network with 2 ports.
1 for frontend which is localhost:3000 and 1 for server with localhost:3001
I currently have the build for my frontend, but the problem here is i wanted to access the localhost:3001
On localhost:3001 which is for development and not for deployment
. As you can see in the image, I can communicate with localhost:3001
But when i try the **BUILD FRONTEND WITH 192.168.254.100:300 ** It can't find my server (port 3001)
EDIT
I'm successful on solving the first problem. But, another problem is i can't access the site on using a mobile phone without cors extension from chrome. And as well as the COOKIES ARE NOT SETTING in order to be authenticated.
This problems occurs due to the CORS policy. Basically, when you send a request from a URL that is different from your server URL, you'll be blocked. For example, if you communicate to your server using its own URL (localhost:3001), it works fine. However, if you do that from your frontend URL, CORS policy will block that. Therefore, in your server, you have to add Access-Control-Allow-Origin in your HTTP response. In Express, you can add the cors middleware.
First, go to your server folder and install cors:
npm i cors
In your entry point file of your server (server.js, index.js, or whatever), insert this line:
app.use(cors()); //This allow any websites to communicate to your server
If you want to only allow your frontend url to communicate and not others:
app.use(cors({ origin: "http://192.164.254.100:3000" })
app is of course from:
const app = express()
Restart the server and it should works fine.

How to change path of socket.io in socket.io.slim.js

I am using socket.io for multiuser experience in one of our metaverse projects. We have deployed the code in a https server and after deploying the multiuser experience collapsed. When check for errors, we have found the following error.
enter image description here
enter image description here
we need to change the path of Url for where it needs to look for socket.io.
Can someone suggest any ideas for changing this path?
This usually means one of 2 things:
the socket.io server is not running
the socket.io server is running on a different place
Make sure socket.io is using the HTTPS server, not the HTTP one, and the client is connecting to the correct domain.

Send HTTP request through browser to a program that is listening on a port

So I am trying to build a C Program that handles HTTP requests through a web browser. The program is listening on a predefined port for incoming HTTP requests and responds appropriately. It is working totally fine when I place is it in the root directory but it doesnt work when I move it to directory on its own.
For instance, this works:
localhost:9000
but this doesnt:
localhost:9000/myprogram
Any clues on how I may fix this?
Hard to answer with so few details. But to get started I would check the following:
What does the web server log say?
Is the web server configured to allow execution of [CGI] in this directory?
Are you sure you haven't mixed up the web root with the directory root, thus placing your
program in the wrong place?
Are the directory and program access modes set correctly?

HTML5 php websocket

I'm new to HTML 5, websocket. We are trying application like white board.Users who all are logged in that particular session can type their thoughts in white board div. It should display to all users who are logged in that session. So i tried to use Php Websocket. what all are the steps need to follow to run php Websocket on server side. Let me know further clarification.
Thanks,
Dhinesh.B
Find some PHP classes that implement the latest Websocket protocol RFC6455. This is an example implementation that I tested and that is working just fine: https://github.com/esromneb/phpwebsocket . Copy those files to your websocket subfolder (e.g. /websocket)
The steps are:
Run the server side PHP (e.g. php -q websocket.demo.php). Use a previously unused socket port.
Connect to the server using a client HTML with Javascript websocket code. In your Javascript code use the port that you specified for the server (e.g. localhost/websocket/client.html)

HTML5 WebSockets Only Work For Localhost

I am having a bit of a problem with HTML5 & WebSockets.
I have put together a very simple client / server application to pass strings to each other and all works well when I am accessing the client html page via a local file path or via http://localhost, however when I try and call it from http:// or http:// it doesn't seem to work.
The first thing the client does is check if the browser supports Web Sockets:
if (!window.WebSocket) {
_Status.innerHTML = "Web Sockets not support by this browser";
return;
}
When I browse to the page with the local host address window.WebSocket is true, but when I use anything else, the page works but window.WebSocket is false.
Am I missing anything obvious? Is it an IE permissions/trust issue?
I am using IE10 and Windows 8 Consumer Preview.
I am out of ideas. Has anyone got any ideas?
WebSockets uses TCP protocol layer to connect.
Server opens Socket and Binds it to specific Port. Then it listening port and accepting connections.
If you are trying to connect to your server, make sure that you use right external IP and you probably need to set up Port Forwarding on your router to your computer, that router will know where to redirect packets.