Fail to use socket.io not found - node.js

My structure
game.js
node_modules\
public\
|index.html
|js\main.js
game.js is nodejs server and in plubic folder is client side
In index.html I have
<script src="/socket.io/socket.io.js"></script>
But when i run in web browser it say
GET http://localhost:1000/socket.io/socket.io.js 404
(index):3 Uncaught ReferenceError: io is not defined
at (index):3
But when i download socket.io.js and make like this
game.js
node_modules\
public\
|index.html
|js\main.js
|socket.io\socket.io.js
And run again it localhost:1000 say
localhost:1000/socket.io/?EIO=3&transport=polling&t=Lg5U91n 404 (Not Found)
what is mistake. I run in window. How to fix that thank.

You will need to include socketio-client on the client side via the script tag. That will allow IO to be defined.
<script>
var socket = io.connect('http://localhost:1000');
socket.on('connect', function(data) {
//Do Something here
});
</script>
You will also need to make sure on your backed (the server that socketio is connecting to) has socketio properly defined and is being used in the HTTP server. An example would be:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
server.listen(1000);
io.on('connection', function(client) {
console.log('Client connected...');
//socket code here
});
Its very hard for us to help you without any code sample of what you are working with. The example provided is a very BASIC way of getting socketio up and running.
Checkout the socketio client github that has a quick and simple demonstration of what to do as well:
Socket.io Client Github

Related

Socket.io serve 404 for non-socket.io urls

We have some kind of chat app using nodejs and socket.io as server. Client is just a regular PHP based app. It is working quite well.
However, we would like to serve 404 errors for URLs not associated with the socket.io app, like the base url, favicon.ico and the rest of the urls.
For example, the server is at:
https://awesome-messenger.com
and the usual socket.io url is at:
https://awesome-messenger.com/socket.io/*
We would like to throw 404 to anything else not handled by socket.io.
Current setup:
nodejs 6.x
socket.io latest
Sample code (messenger.js):
var app = require('http').createServer()
var io = require('socket.io')(app);
app.listen(4040);
io.on('connection', function (socket) {
socket.on('joinRoom', function(data){
socket.join(data.room);
});
// Other codes here...
});
Started by:
node /path/to/messenger.js
Thanks!
We used express + socket.io instead to serve a custom homepage and throw 404 errors on non-socket.io urls. I got the idea from here: http://socket.io/get-started/chat/
var app = require('express')(),
http = require('http').Server(app),
io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.on('joinRoom', function(data){
socket.join(data.room);
});
// Other codes
});
http.listen(4040, function () {
console.log('Listening on *:4040');
});
So if we access something like: /favicon.ico or /foo/bar, it will throw a simple 404 error.

Socket.io doesn't correctly attach to http server in express application

I'm pretty new to NodeJS and trying to get Socket.IO running with an Express application. I've tried to stick to the docs and tutorials as close as possible, but in the end, socket.io.js cannot be found on client side.
The server is being started as follows:
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() { debug('...') } );
Routing/serving pages:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('index', { title: 'Express' });
});
Including socket.io:
var express = require('express');
var http = require('http').Server(express);
var io = require('socket.io')(http);
And in the HTML file, for clients:
<script src="/socket.io/socket.io.js" type="text/javascript">
But when loading the page, this JS-file is never found. I suspect that including socket.io via that previously created http server is somehow wrong... Or am I missing something else?
Update
I just found out that the snipped that includes socket.io is executed before the server is started (first snippet). They're in different files and the server-start one (named www.js) is configured as "start" script in package.json, the other one (websockets.js) required indirectly by that.
If I put
var io = require('socket.io')(server);
at the end of www.js, everything works just fine. But is there a way to do it from websockets.js, which is loaded before, e.g. by a server start callback function?
Try specifying the full path, like this
<script src="http://localhost:8000/socket.io/socket.io.js"></script>
And connect to your server from client side:
var socket = io.connect('http://localhost:8000/');
also check if socket.io module is available in your node_modules directory.

Do I need socket.io-client on browser-side of things?

EDIT: The answer appears to be no.
I'm new to Node.js, bower and Socket.IO and I'm not sure what I need for my purpose.
I'm making an app that has a frontend (where browsers connect) and a backend (a single Node.js server).
What do I need to create a Socket.IO server instance on the backend? What do I need on the client-side? Does the Socket.IO package contain both?
First install socket.io using below command
npm install socket.io
and the call socket.io in your server js File
var io = require('socket.io');
And the create connection in your server js file
var app = express();
app.get('/', function(req, res){
fs.readFile('index.html', function(Error,data){
res.writeHead(200,{'Content-Type':'text/html'});
res.write(data);
res.end();
});
});
server = http.createServer(app);
var IO = io.listen(server);
server.listen(3000);
IO.sockets.on('connection', function(socket) {
socket.on('msg_to_server', function(data) {
console.log(data);
});
});
Add this script inside head Tag in your index.html
<script src="/socket.io/socket.io.js"></script>
in your index.html create socketio connection
var socketio = io.connect("127.0.0.1:3000");
send some data to server following way
socketio.emit('msg_to_server',{ message : 'some data' });

Can't get socket.io.js

I'm actually working on a little project, where i'm supposed to recreate a drawing multiplayer game with node.js, mongoDB, socket.io and canvas.
The drawer is working like a charm, and the server seems to work well too. I got my register/login/sessions and database up and working, the only problem is socket.io. When an user is joining the game room, he can see the drawer and tools, but no connection. Why ? The browser can't find socket.io.js.
What I did :
I verified if it was installed, it is with npm install socket.io.
I checked if the server was starting it when turning the server on : Got "socket.io started" in my console.
I checked my HTML code, here it is :
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
According to the billions of tutorials/dev sites/help subjects, this is supposed to work. But it's not. When opening the console of my browser, I got this :
X GET http://localhost:1337/socket.io/socket.io.js NOT FOUND.
I don't know where is the problem, I can't figure this out and it's giving me a huge headache.. So I'm here.
Thanks in advance for helping ! :)
Given the code in your comment, you're not using the correct variable for initializing socket.io.
Try this:
var express = require('express');
var app = express();
var server = app.listen(1337);
var io = require('socket.io').listen(server);
...
So instead of having socket.io 'listen' on the Express app instance, it should listen to what app.listen(...) returns (which happens to be an http.Server instance).
For anyone landing here because they're going through the v4.x socket.io get started example, all you need to do is add another endpoint to your index.js file
index.js
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// add this
app.get('/socket.io/socket.io.js', (req, res) => {
res.sendFile(__dirname + '/node_modules/socket.io/client-dist/socket.io.js');
});
///
io.on('connection', (socket) => {
console.log('a user connected');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});

cannot get client server running using express in node.js

hey i just started tinkering with node.js and am a complete noob. i am trying to get a simple client server communication going using socket.io and express (i have never used these before).
here is my code for the app(app.js):
var sys = require('sys'),
express = require('express'),
app = express('localhost');
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.send('Hello World');
});
app.listen(3000);
var socket = require('socket.io').listen(server);
socket.on('connection', function (client){
// new client is here!
setTimeout(function () {
client.send('Waited two seconds!');
}, 2000);
client.on('message', function () {
}) ;
client.on('disconnect', function () {
});
});
and here is my code for the client(client.html):
<html>
<p id="text">socket.io</p>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function(){
var socket = new io.Socket(),
text = $('#text');
socket.connect();
socket.on('connect', function () {
text.html('connected');
});
socket.on('message', function (msg) {
text.html(msg);
});
socket.on('disconnect', function () {
text.html('disconnected');
});
});
</script>
i got most of the code from:
NodeJS + socket.io: simple Client/Server example not working
and the changed it to be compatible with express 3.x
however when i run the server and open my client using chrome it tells me that it is unable
to load resource file:///socket.io/socket.io.js
i have already installed express and socket.io using npm
also i have read through atleast 20 similar posts and have not been able to find an answer
please help me. thank you
socket.io.js file needs to be served from port 3000, like localhost:3000.
So here is what you do change
<script src="/socket.io/socket.io.js"></script> to
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
Are you opening the client.html page directly from the local file system? The request for socket.io.js should look like http://localhost/socket.io/socket.io.js not file:///socket.io/socket.io.js.

Resources