This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
socket.io: Failed to load resource
A simple express + socket.io hellow world app seems doesnt work, i keep getting
"NetworkError: 404 Not Found -
http:// localhost:3002/socket.io/socket.io.js"
Anyone know this issue?
code:
app.js
var express = require('express'),
http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.configure(function () {
app.use(express.static(__dirname + '/public'));
});
app.listen(3002);
io.sockets.on('connection', function(socket) {
socket.emit('news', {hello: 'world'});
socket.on('my other event', function (data) {
console.log(data);
});
});
index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my:'data' });
});
Your Socket.IO server is listening on port 3002, but you connected to:
var socket = io.connect('http://localhost');
which by default uses standard port 80. Try this:
var socket = io.connect('http://localhost:3002');
Also you will probably have to change localhost to the IP of your machine.
Side note: If you are running Express Server and Socket.IO server on the same port, then you can simply do:
var socket = io.connect();
Related
I am trying to use socket.io in my server. But it gives me this error: GET http://localhost:4000/socket.io/socket.io.js net::ERR_ABORTED 404 (Not Found)
Here is my server code:
const app = express();
const http = require('http');
const server = http.createServer(app);
var io = require('socket.io')(server);
const taskRouter = require("./routers/tasks.js")
app.use( "/" , taskRouter)
io.on('connection', function (socket)
{
console.log("Connected!")
})
app.listen(PORT, () => {
console.log(`Server is on port ${PORT}`);
});
Here is my pug code:
extends ../header2.pug
block unique-css
include ../../public/css/mentor/chat.css
block unique-content
.main
h1 Hello World!
script(src="/socket.io/socket.io.js")
script
include ../../public/js/mentor/chat.js
Here is my client-side javascript code:
var socket = io()
socket.on('connection', function (data) {
console.log("helloworld");
});
And the problem is when I try to use socket.io I get this errors in browser GET http://localhost:4000/socket.io/socket.io.js net::ERR_ABORTED 404 (Not Found), Uncaught ReferenceError: io is not defined. The second error is pointing to the first line of frontend js code.
Use server.listen(port)
const app = express();
const http = require('http');
const server = http.createServer(app);
var io = require('socket.io')(server);
const taskRouter = require("./routers/tasks.js")
app.use( "/" , taskRouter)
io.on('connection', function (socket)
{
console.log("Connected!")
})
//changes below
server.listen(PORT, () => {
console.log(`Server is on port ${PORT}`);
});
And in the client-side you don't require this code. Delete this.
socket.on('connection', function (data) {
console.log("helloworld");
});
So, I have this app called server and the other one called client, server providers all the data for the client to consume. The thing is, when i try to emit from server (port 8080) and receive to client (port 80) nothing happens
server: app.js
var app = require ("./config/server.js");
var http = require('http').createServer(app);
var io = require("socket.io")(http);
http.listen(8080, function(){
console.log('Server side instagram_clone_v01 online');
});
io.sockets.on('connect', function (socket) {
console.log("conectou socket.id="+socket.id);
});
When the server database insert new photo, this is called:
io.emit("newPhoto");
client: app.js
var app = require('./config/server');
app.listen(80, function(){
console.log('Server client instagram_clone_v01 online');
});
var io = require('socket.io');
This is called inside a ejs code:
const socket = io.connect('http://localhost:8080', {transports: ['websocket', 'polling', 'flashsocket']});
socket.on('newPhoto',function(){
load_posts();
});
Edited with the Answer of Federico
I added io.origins('*:*'); to server, but emit isn't emitting
I don't know why you are using io.sockets.on, I couldn't find it in the documentation. I've tried to clean the code, give it a try.
server.js
var app = require ("./config/server.js");
var http = require('http').Server(app);
var io = require("socket.io")(http);
http.listen(8080, function(){
console.log('Server side instagram_clone_v01 online');
});
io.on('connect', socket => {
console.log("user" + socket.request.user.id + "connected");
socket.on('disconnect', function() {
console.log('A user has disconnected');
}
io.emit("newPhoto");
});
client.js
//io() works only when connecting to a socket hosted on the same url/server
// For connecting to an external socket hosted elsewhere, you would use io.connect('URL');
var socket = io();
socket.on('newPhoto',function(){
load_posts();
});
In the page where your user being redirected after login, you should include these scripts:
<script src="/socket.io/socket.io.js"></script>
<script src="/client.js"></script>
I am trying to build a small websocket application. I am trying to implement it on a website that already runs on a secure HTTPS protocol thus the websocket has to be on wss to work.
I build the server using the following code:
var express = require('express');
var app = express();
var fs = require('fs');
var key = fs.readFileSync('/etc/apache2/ssl/www.bigriss.com_private_key.key');
var cert = fs.readFileSync( '/etc/apache2/ssl/www.bigriss.com_ssl_certificate.crt' );
var ca = fs.readFileSync( '/etc/apache2/ssl/bigriss.com_ssl_certificate_INTERMEDIATE.crt' );
var options = {
key: key,
cert: cert,
ca: ca
};
var https = require('https').createServer(options, app);
var io = require('socket.io')(https);
var port = process.env.PORT || 8080;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
var token = socket.handshake.query.token;
io.emit('chat message', msg + token);
});
});
https.listen(port, function () {
console.log('listening on *:' + port);
});
Which would seem to me to be a quite simple and standard implementation. I can access the url https://example.com:8080 properly and it is giving the contents that are read at index.html. On this html file is where I try to establish a connection using:
var socket = io.connect('https://bigriss.com:8080/?token=abc', {
transports: ['websocket'],
upgrade: false,
secure: true,
reconnect: true,
rejectUnauthorized : false
});
Having played several times with the options. Regardless, I get an error in the Chrome Console as being:
failed: Connection closed before receiving a handshake response
failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
In the terminal I get the following node error:
nodejs: ../src/util-inl.h:196: TypeName* node::Unwrap(v8::Local<v8::Object>) [with TypeName = node::TLSWrap]: Assertion `(object->InternalFieldCount()) > (0)' failed.
Aborted
I have testing this same setup with HTTP and it works correctly. Is there anything in particular that I am missing with HTTPS?
this is the test case...
app.js
var express = require('express'),
http = require('http'),
//other variables
var app = express();
var server = http.createServer(app);
var io = require("socket.io").listen(server)
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
html_index.js
$(document).ready(function()
{
var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
});
});
i get this error
WebSocket connection to 'ws://localhost:26000/socket.io/1/websocket/%3Ch2%3Efile%20not%20found!%20error%20404!%3C/h2%3E' failed: WebSocket is closed before the connection is established. socket.io.js:2438
WS.close socket.io.js:2438
Socket.onDisconnect socket.io.js:1967
Transport.onDisconnect socket.io.js:1362
Transport.onClose socket.io.js:1456
(anonymous function)
im using
express 3
socket.io 0.9
nodejs 0.10
the problem was the port
this is the solution
var server = app.listen(26000, function(){ //instead of var server = http.createServer(app);
console.log("Express server listening on port %d in %s mode", app.get('port'),
app.settings.env);
});
//SOCKET.IO
var io = require("socket.io").listen(server)
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
});
I am starting to learn how to use socket.io and I've been trying to figure this out for the last couple of days but I don't get 2 things.
Why, after starting the server and loading my respective url (localhost:8088) does it take so long for my alert to show up?
Why can't I see the server code of socket.io? for example in the next chunk of code I never see in console my data from "my other event".
server (app.js):
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(8088);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', {
hello: 'world'
});
socket.on('my other event', function (data) {
console.log(data);
});
});
client (index.html):
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on('news', function (data) {
console.log(data);
alert(data.hello);
socket.emit('my other event', {
my: 'data'
});
});
</script>
It might be the incompability between express 3.0 and socket.io
Try this:
var express = require('express'),;
var http = require('http');
var app = express();
var server = module.exports = http.createServer(app);
var io = require("socket.io").listen(server);
server.listen(8088);