Socket.io is not defined in console when hosted - javascript

I am a newbie to socket.io and hosting on a Raspberry Pi so I come here to ask help!
I currently want to run a live chat on my web-page which is being hosted on apache2 with the domain of tuckermedia.org. When I run it locally, I do not get the error when I run nodemon server.js and it runs at it should but this doesn't seem to work on the Raspberry Pi.
When I host it on my Raspberry Pi, I constantly get this error: Uncaught ReferenceError: io is not defined (Screen Shot below)
[https://i.stack.imgur.com/asfu2.jpg][1]
Now, I have changed my script SRC to: <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script> This is what it currently is.
This however still gives me the same issue. I ensured that I have socket.io, nodemon, and the other packages that I needed. I also ensured that I started the server with nodemon but I still get the same error.
Nodemon screenshot on Raspberry Pi:
https://i.stack.imgur.com/Ao98P.jpg
If you can help me out, thanks! You can also message me on discord at Logans#9830 if you would like to have a conversation. Thanks!
HTML Code: https://pastebin.com/aGhqFFm8
script.js (client side) code: https://pastebin.com/XkXJPDb2
server.js (server side) code: https://pastebin.com/YYvK1dwd
Here is the console sources on my Raspberry Pi compared to it being hosted locally.
https://i.stack.imgur.com/oHndj.jpg
It seems that some of my files don't even show up like they should.

There's should be a correct link to socket.io at your frontend code. I suppose your path to script is the following one:
<script src="/socket.io/socket.io.js"></script>
Try this one:
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
Source: https://cdnjs.com/libraries/socket.io

Related

How to stop a NodeJs file on Ubuntu server

I paid someone to install a Js script on my server
but when i tried making some changes on scripts/files i just couldnt see the changes on browser
while browsing and googling for like 24 hours i figured out that js scripts should be restarted in order to show the result on browser
and i figured out that in order to start a node js file i should type in terminal
node /dir/example.js
but can someone help me how to refresh/restart it
Iam stuck here like 24 hours
please help
I just want to let you know too that iam using Putty software in order to connect to the ubuntu server, ubuntu version is 20.04
EDIT: you will need an ssh connection to your server with access to a terminal..
Your node.js scripts, if they are on a server, should typically be ran using a process manager such as pm2.
Try to type pm2 list in a terminal. If you see some information you have hit the spot. Just after that run pm2 restart id. id being the number of your process and your changes should be visible.
You can just try this or try your luck with other process managers that run on your server providers. Just read what your server provider recommends for default and usually your previous developer would have used it.

Running Nodejs on Managed Hosting server but the website goes down after some interval

I am facing this problem from last two weeks.
I am accessing and running server from puTTy terminal. Earlier i was facing problem that when i close my puTTy terminal the website showed 503 error, despite of using 'nohup' command.
nohup npm start --production &
Then i tried using 'Screen' , now if i close my terminal , the website is still live but after some hours (20-24) the Screen is dead and the server is also down.
Please give me a solution to run node.js app in production without the hassle of this 503 error

Node.js /socket.io/socket.io.js not found express 4.0

So I'm trying to get chat working on my website, and when I was testing locally it worked great, because port 8080 on my localhost was available and all that good stuff. But now I pushed my code to my Heroku app, and when I try and load my chat page, I get the error stating that it can't get localhost:8080/socket.io/socket.io.js.
I've seen node.js /socket.io/socket.io.js not found
and tried the suggestions, but none worked, even moving the socket.io.js file into a resource file did not work. I'm guessing this is because I'm using express 4.0?
Any help would be appreciated
Thanks
Edit:
So to add more details, since my question could seem a little vague, here is my relevant app.js code:
var client = require('socket.io').listen(8080).sockets;
In my jade file for the chat page, I have:
script (src = `'http://localhost:8080/socket.io/socket.io.js`')
and later on
var socket = io.connect(`'http://localhost:8080`');
and all this works on localhost (I load up on port 5000, socket.io is connected to port 8080). I do this using 'foreman start' with the heroku toolbelt.
When I try and change these to work on heroku, it breaks and I'm not sure how to fix it. I hope this clarifies the question a bit.
Edit 2:
I'm running:
express 4.0.0
socket.io 0.9.16
node 0.10.x
Thanks
Do you have an explicit route in express which catches all other routes? Something like this perhaps:
app.get("/", handlers.home);
app.get("/..." ...);
...
app.get("*", handlers.error);
This might keep socket.io from being able to host it's own js file for the client. There is an easy way to fix this, since you probably already have a public or static folder setup in express. Something like:
app.use(express.static("public"));
Make a new folder called socket.io and copy over the appropriate socket.io.js file into said folder, and all should be well. However note that there are two files named socket.io.js!! So, if you see something like "Uncaught ReferenceError: require is not defined" it means you copied the "node-ey" server side file. Here is the correct client file to copy:
app_dir/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.min.js
Note #BHendricks: I would have just posted as a reply to your comment, but I currently lack the required reputation.
Edit:
The OPs question probably has more to do with the "localhost" issue. When connecting from a client (say your home IP), as far as your browser knows - localhost implies a connection with the machine which is locally hosting stuff. Since your home machine (or phone) does not host socket.io, this is failing.
What you need to do is have your server embed the socket connection information (either a fully qualified hostname, ip etc). This can be done when the server "renders" the page with the client connection.
What happens when you go to http://localhost:8080/socket.io/socket.io.js?
Does it 404? If it does you need to make sure you have it in a directory that Express is set to serve statically.
app.use(express.static(__dirname + '/public'));
Then put your socket.io.js file in public/socket.io/socket.io.js (relative to your app.js file)
Restart your server and see if that fixes it.
Basically, Express doesn't serve files statically from the file system unless you explicitly tell it where to map from.

An Apache Web Server Message [duplicate]

This question already exists:
How to run d3 Javascript
Closed 8 years ago.
I am trying to set up d3 Javascript by following this link:
https://github.com/mbostock/d3/wiki . I am currently using Eclipse Juno and Tomcat in Mac 10.5.8. basically I am trying to run a simple Force-Directed graph example that was taken from the following link:
https://gist.github.com/mbostock/4062045
I have been recommended to insert the command below into the Terminal in order to get d3 run:
python -m SimpleHTTPServer 8888 &
With the Web Browser (Firefox in my case), I can insert:
http://localhost:8888/
. When I do this, I get the following message in the browser:
If you can see this, it means that the installation of the Apache web server software
on this system was successful. You may now add content to this directory and replace this page.
Seeing this instead of the website you expected?
This page is here because the site administrator has changed the configuration of this
web server. Please contact the person responsible for maintaining this server with questions.
The Apache Software Foundation, which wrote the web server software this site
administrator is using, has nothing to do with maintaining this site and cannot help resolve configuration issues.
The Apache documentation has been included with this distribution.
You are free to use the image below on an Apache-powered web server. Thanks for using Apache!
What that message means and why am I getting this type of message? How can I get the d3 run so long as I am getting this message?
It looks like that Apache and python are competing for the same port and since you start python in the background mode, it is not reporting the errors correctly.
Try starting python -m SimpleHTTPServer without an argument. It should start on port 8000 and serve the files from the folder it is started from.
The next steps would be to move the html file and the json file in the same folder where you started the server from. Then point your browser to http://localhost:8000/<name-of-your-html-file>.html.

SocketIO can not serve static content after I upgraded nodejs

I upgraded my nodejs from 0.6 to 0.10
before socketIO works fine, but now whenever client request "socket.io.js"
"http://localhost:49991/socket.io/socket.io.js"
The response is only one line.
Welcome to socket.io.
Not the actual file "socket.io.js". My app crash because of this.
The output of sever console seems to be fine:
debug - served static content /socket.io.js
My socket.io version is 0.9.*. What could possibly be the problem here?
EDIT:
found this is duplicated:
Socket.IO client library gives "welcome to socket.io" message

Categories

Resources