socket node.js express 404 error - node.js

From a simple chat example at https://github.com/guille/chat-example/blob/master/index.js
For information, I'm not serving html file through following code, either the html is going to be embedded inside this iphone app or hosted somewhere. Question is how to connect the html to the nodejs environment where the sockets will be handled or is it a must that the html goes through the res.sendfile statement?
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
html
<script src="js/api/jquery-2.1.1.min.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
var socket = io();
socket.emit('chat message', "hello world");
});
</script>
I'm getting a 404 error, attached screenshot
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log(msg);
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});

You are using res.sendfile when it should be res.sendFile (capital F)

Related

how to pass a param to socket chat client through browser url

I just created a small socket.io chat and my server code is like
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
socket.broadcast.emit('Welcome to Dobuyme');
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
An my client side in the index.html is
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
});
Now i can connect the chat server through the url
http://localhost:3000/
Now i want to integrate this module with my program, for that i need to pass a user id to this url.i just did something like
http://localhost:3000/index.html?id=2
But i cant pass the id.How can pass an id variable to node server through the url

connection between client-side and server-side not working on socket.io

I have problem with socket connection between client-side and server-side .
I have this code on server-side :-
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
and this code on client-side :-
<html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
the code in server-side is working , but in client-side i have these errors :-
can anyone help me to fix problem ?
Thanks a lot :(
Its looks like you are opening file directly into browser. Actually you are not specify path for serving your html. so that socketio can POST and poll data there.
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
If you really need to open file directly into browser rather than serving via nodejs on same port you have to specify host and port in client-side socket.
rather than simply using io() which will look for local port you can use this
var socket = io("http://localhost:3000");
So here how it works for me
// server
const app = require('express')();
const http = require('http').createServer(app);
// ^^^^^^^^^^^^
const io = require('socket.io')(http);
// here you should serve your index.html file
// and the socket io will know which socket to track
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
// client/index.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<script src="/socket.io/socket.io.js"></script>
<script>
// here it automatically track localhost:3000
var socket = io();
</script>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>
Hope it will help you

Nodejs with socket.io getting error on connection in AWS server

I am implementing a chat application using nodejs and socket.
Which is working perfect on localhost but not on live server(AWS). I had install socket, node and express js on server.
<script src="http://50.112.30.188/chatDemo/node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
And in index.js file
var express = require('express'),
http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
console.log('listening on *:3000');
The error i am facing is
"http://50.112.30.188/socket.io/?EIO=3&transport=polling&t=1475061508132-0" not found as this is called continuously after running node from terminal with the cmd: node index.js.
i had also tried by add port in io() but makes no difference. So can any of can help me out from this problem as i am new in nodejs. That will be appricated.

Moving socket.io into controller

This is my app structure
controller
routes.js
public
jquery.js
socket.io.js
app.js
index.html
The app.js file contains the socket program which emits the some message when user connects. Now the problem was how I emit the message from routes.
My nodejs code was below:
This is my app.js file code:
var express=require('express');
var app=express();
var server=require('http').createServer(app);
var io=require('socket.io').listen(server);
app.use('/', express.static(__dirname + '/'));
io.sockets.on('connection',function(socket){
socket.emit('message',{msg:'hai'});
});
app.use('/cal',require('./controllers/route'));
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
server.listen(3000);
This is my route.js code:
var express=require('express');
var route=express.Router();
route.get('/',function(req,res){
//socket.emit('message',{msg:'hai'});
});
module.exports=route;
This is my index.html file code:
<htmL>
<head>
<title>Chat with socket.io and node.js</title>
</head>
<body>
<script src="./public/jquery.min.js"></script>
<script src="./public/socket.io.js"></script>
<script>
jQuery(function($){
var socket = io.connect();
socket.on('message',function(data){
alert(data.msg);
});
});
</script>
</body>
</html>
Thank You.
Routes has nothing to do with sockets. You have to listen on sockets, not on routes. Here's par of the docs (http://socket.io/docs/) for using Socket.IO with Express:
// server - app.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
// client - index.js
<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' });
});
</script>

Is there any work arounds with sending value to php file using socket.io?

I am trying to send a value with socket.io to a php file. I know that node.js will not interpret a PHP file.. I was just wondering if there any work around for this
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/order_management.php', function (req, res) {
res.sendFile(__dirname + '/order_management.php');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
console.log('message: ' + msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});

Resources