Socket.io 404 Not Found
Change this:
app.listen(3000);
to:
server.listen(3000);
app.listen()
creates a new and different server so the server that you have socket.io attached to is not the one that is actually listening and thus your socket.io server is not live and not able to handle the /socket.io/socket.io.js
request or the incoming connection if the script file didn't first fail to load.
See this answer for more details on what app.listen()
does and why it isn't what you want the way your code is laid out:
websockets, express.js and can’t establish a connection to the server
Note, you could use app.listen()
, but you'd have to not create your own server and you'd have to capture the return value from app.listen()
and use that as the server you pass to socket.io initialization:
var app = express();
var server = app.listen(3000);
var io = require('socket.io')(server);
Which is not quite how your code is structured (but you could rearrange things to do it this way).
Socket.io - socket.io.js not available
I've never worked with AIOHTTP (always worked with flask-socketio) so I did some research using the documentation. It turns out that like a lot of http server, AIOHTTP doesn't serve static files out of the box.
You will thus need to take care of it using the add_static
method (doc here).
(Note that if you want to deploy this app for production it would be better practice to use an HTTP server like Apache or Nginx).
Just add this line under your routes declarations.
app.router.add_static('/static/', path='static/')
Now you need to create a static
folder in the root of your project and put your socket.io.js
file inside of it.
Make sure that the socket.io.js in the version 2.2 (same as the CDN version). You can get this file by downloading it directly from the CDN URL you were using or using npm -i -s socket.io-client@2.2
. You can then find the socket.io.js
file in node_modules/socket.io-client/dist
.
You now just need to use <script src="/static/socket.io.js"></script>
in your HTML to import socket.io.
socket.io.js not loading
Turns out the reason I could never request the .js file was because my company network blocks all ports except the usual ones (80, 21, etc), so `I would never be able to communicate with port 8000.
Socket.io.js is not loaded
I think you should try and break it down a little bit to identify your problem. You are clearly not reaching your socket.
Try to have a server just like this:
var express = require('express')
var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
io.on('connection', function(socket) {
console.log('Socket did connect');
});
server.listen(8080)
And Client:
var socket = io();
If this works, which it very much should, try to add the rest of your code. Ejs shouldn't be the problem since it's compiled to regular HTML before it's served.
A problem would be if you have you client running on a different domain (or port) than the server. If so, you would have to define it inside the io() on the client site, eg: var socket = io("http://localhost:8080")
Client side script src=/socket.io/socket.io.js/script not found, using Express-generator generated project
After a long time searching, I have found the best answer to my question. Please refer to A simple express socket.io tutorial using express generators.
Related Topics
How to Split a String into an Array of Characters
Getting a Unhandledpromiserejectionwarning When Testing Using Mocha/Chai
Unsafe JavaScript Attempt to Access Frame with Url
How to Access Parent Window Object Using Jquery
How to Beautify JavaScript Code Using Command Line
Create a File Using JavaScript in Chrome on Client Side
Map Isn't Showing on Google Maps JavaScript API V3 When Nested in a Div Tag
How to Scroll the Window Using Jquery $.Scrollto() Function
JSON Stringify Changes Time of Date Because of Utc
Recursively Print All Permutations of a String (Javascript)
How to Accomplish an If/Else in Mustache.Js
Jquery Add Required to Input Fields