What happens if node js doesn't find a client html file? - javascript

AM trying to implement a push server for my pHP application.
My initial thoughts were as long as i keep the EVENTS called on my client let say message_view.php file . I would not have a problem of emiting node js events.
But i see that most of the tutorial online use something like I dont understood this ?
fs.readFile(__dirname + '/client.html') ...//html files
or
response.sendfile('hello world')
After they have started the server. Then they add event and logic as follow on the client html file that am supposed to do it on my messages_view.php file.
<script src="socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
and socket emiting events like this one :
<script>
// create a new websocket
var socket = io.connect('http://localhost:8000/?profile_id=abcde2');
//..I want to code here to update my divs.
</script>
What do i need exactly to emit this message on my PHP file. Many Thanks!

If you want to serve html with php, and have a node.js socket server, the socket server doesn't need to serve html, so you can omit the majority of the example you're using. Just include the script in your .php that serves the html page and make sure the url to the script properly targets the socket server.

Related

Socket.IO Getting 404 Errors on Some URLs, but not others

I am trying to use Socket.IO in my web application and it has worked great so far. However, I have been trying to fix one specific issue for a long time and have not been able to find anyone else having the same issue. Socket.IO works great on URLs where it is just site.com/example, however, when I stack paths on the domain, I get a 404 in socket.IO. For example, site.com/user/example displays a 404 for socket.IO. In the log, it tries to access socket.IO at site.com/user/socket.io/... when it needs to access it at site.com/socket.io. It seems to only replace the url after the last / so site.com/ex/a would make it try to get socket.io at site.come/ex/socket.io.(I am using ExpressJS, I didn't know if that was relevant.)
I have tried to set the path and resource for socket.io to use in the client script. Also, I included the path when binding the socket.io instance to the http server.
Here is my server side code.
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server, {path: '/socket.io'});
Here is my client side code.
var socket = io.connect('https://example.net', {
path: '/socket.io',
resource: '/socket.io',
transports: ['websocket'],
upgrade: false
})
Thanks so much for all help!
(I am sorry for any incorrect formatting of this question, this is my fist time asking on StackOverflow!)
You likely need a leading slash on the socket.io script tag.
The big clue is when you said your socket.io script loads when the page URL is https://example.net/dashboard, but doesn't load when it's https://example.net/user/fludo. And, the screen shot shows it trying to load the script from https://example.net/user/fludo/socket.io/socket.io.js which is, indeed, the wrong path.
The problem is that your <script> tag is using a page relative link with no leading / on the URL. That means the browser will combine the path of the page URL with the filename in your <script> tag. But, you don't want to use the path of the page. You want to load it from the same place every time. So, to do that, you change from this:
<script src="socket.io/socket.io.js"></script>
to this:
<script src="/socket.io/socket.io.js"></script>

Adding Refernce to a Javascript file in a plain .js file

I want to create a SignalR client in JavaScript. Am not using any HTML files(its not a web application).My intention is to create ".js" file and connect to the SignalR hub and subscribe to the events,and when the events got triggered I want to output the data to a console.
So how can I refer "signalR-2.1.0.min.js" in my plain .js file.
<script src="Scripts/signalR-2.1.0.min.js"></script>
The above way of reference works only with HTML files.This is working fine in my web application.
But if I want to run a ".js" file for the same purpose (as a SignalR client without html files)how can I add reference to this "signalR-2.1.0.min.js" file?
Thanks In Advance
Susmitha
If you your using Node.js, you can just use require:
const myFile = require('./myScript.js');

What did I just implement with Node.js?

I'm following this tutorial, I was confused at the point where it says:
"...
Surprisingly the code is very simple:"
// Connect to the socket.io server
var socket = io.connect('http://localhost:8080');
// Wait for data from the server
socket.on('output', function (data) {
...
I'm not sure where to put this code. I tried to add it to browser JS, like this:
<html>
<head>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script> <!-- here -->
// Connect to the socket.io server
var socket = io.connect('http://localhost:8080');
// ...
</script>
</head>
<body>
<h1>SSH</h1>
<div class="terminal"></div>
</body>
</html>
And it worked! Is this correct? I thought it was meant to be a server-side code.
Anyways, now I get a terminal which i can interact with. But I'm not sure what it is doing. I was trying to implement an SSH client, but it looks like I obtained an on-browser terminal, over which I will manually connect to SSH?
Also I believe this would only work on the local machine. But what I want is a -remote- web server that can access to my machine using SSH (although it may not be very safe). Am I in the right direction? How can I implement a web server that acts as a client to the SSH server on my machine?
Thanks,
It has absolutely NOTHING to do with SSH in any way, shape or form.
It's a websocket server/client, which allows you to send messages(not commands) between a browser and a server.
It's most commonly used for chat applications, although there are endless other uses.
However, with this mechanism in place, you could interpret certain messages on the server and make them execute the commands you wish to allow your users to use.
Quick example of how it would work (server side) :
socket.on('ls',(path,cb)=>{
fs.readdir(path, (err, files) => {
cb(files);
});
});
and on the client :
socket.emit('ls','/home',(files)=>{
console.log(files);
};
The client here emits a 'ls' event, with a path (user selected or something); and the server interprets this message, get the list of files for the given path, and returns it to the client. This mechanism could be used to implement a variety of commands. But keep in mind that this is NOT SSH.
Read more on Socket.io
If you are following the tutorial the server side code is server.js. This is a simple express.js webserver with a socket.io extension.
The code in the .html file is send to the browser which acts as client.
That's a socket server. It listens for connections from the browser. That's what you're doing in the HTML.

Is it possible to connect to a Socket.IO server from static JavaScript on another server? [duplicate]

I'm serving my page through localhost (XAMPP, Apache), and on my friend's physical server I run a node.js server that is used for communication with the page (a game).
This is the node.js server code:
var io = require('socket.io').listen(1235);
io.sockets.on('connection', function (socket)
{
socket.on("start", function (data)
{
console.log(data);
});
});
It runs without any errors, but I don't know how to include the socket.io code into my webpage! How do I do that?
Include a script tag in your page:
<script src="http://[YOUR IP]:1235/socket.io/socket.io.js">
And it will be served by your node.js server.
Apart from that, you can just follow the examples on socket.io, e.g.:
var socket = io.connect("http://[YOUR IP]:1235");
socket.emit("start", "LET'S GO!");
2 Options. Per the documentation, you can do a JavaScript src pointing at your node server:
<script src="http://url.to.node.com/socket.io/socket.io.js"></script>
Or you can include it manually, grabbing it from the Git repo at https://github.com/LearnBoost/socket.io-client/blob/master/dist/socket.io.js

Python send to javascript

My question is really simpel, but I haven't found it yet.
I have a python script running on my server. This script reads NFC cards. (pyscard)
Everytime when there is a card the cardID must send to a webpage. I have in index.html file on my localhost.
<head>
<title>Hello!</title>
</head>
<script language="JavaScript" type="text/javascript">
function card(Cardid){
alert(Cardid);
}
</script>
<body>
</body>
</html>
There is a way to do it by selenium (driver.execute_script(command) ) Selenium is to heavy. Is there a other simple and light way to do this?
If I got you right, you need to implement AJAX. Here is simpliest example How to implement a minimal server for AJAX in Python?
If you'll need something more complex — chose one of the python web frameworks (Flask or Django) and look for tutorials how to implement AJAX with them.
If you want to push cardId to webpage you need tot use something in between like socket.IO then on a listening channel pushing the data to all websocket clients. From there you can pass it to a function like yours.
The other way to do it is a bit more limiting.
You can make Ajax call to python which reads current card holding data and use that id. But when another card is on the reader,you then have tot refresh to see the new cards data.

Categories

Resources