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

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.

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

socket.io.js not found in express

my folder structure for socket.io is as following:
node_modules/socket.io/
my folder structure for socket.io.js is as following:
/node_modules/socket.io-client/dist/socket.io.js
var express = require('express');
var http= require('http').Server(express);
var io= require('socket.io') (http);
var app = express();
io.on('connection', function(socket){
socket.on('chat', function(msg){
io.emit('chat',msg);
});
});
io.on('disconnect', function() {
console.log('user is disconnected');
});
And view is as follows:
<script src="/node_modules/socket.io-client/dist/socket.io.js"></script>
<script src="/js/jquery-2.1.4.min.js"></script>
<script>
$(function(){
var socket= io();
$('form').submit(function(e){
e.preventDefault();
socket.emit('chat', $('#input').val());
$('#input').val('');
return false;
});
socket.on('chat', function(msg){
$('#sent').append(msg);
});
});
</script>
and errors are:
Failed to load resource: the server responded with a status of 404 (Not Found)
Uncaught ReferenceError: io is not defined
Without creating server you are creating socket instance ,you can pass app to your socket.js or you can export socket to other module and use accordingly....It should be like this
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
First off, a node.js server does not server ANY files by default. For a file to be served by the server, there must be a route handler that has code to send that particular file. So, that explains why this doesn't work:
<script src="/node_modules/socket.io-client/dist/socket.io.js"></script>
because there is no route handler in your server for the path "/node_modules/socket.io-client/dist/socket.io.js".
But, socket.io solves this problem for you. The socket.io server automatically installs a route for:
/socket.io/socket.io.js
And it will reach into that node_modules/socket.io/client directory and send the socket.io.js file when it sees the above request.
So, change your script tag to this:
<script src="/socket.io/socket.io.js"></script>
And, the socket.io server will automatically send the socket.io.js file from the dist directory.
It looks like your server initialization is also not correct because you don't pass express to your http server, you pass app. Change this:
var express = require('express');
var http= require('http').Server(express);
var io= require('socket.io') (http);
var app = express();
io.on('connection', function(socket){
socket.on('chat', function(msg){
io.emit('chat',msg);
});
});
io.on('disconnect', function() {
console.log('user is disconnected');
});
to this:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(80);
io.on('connection', function(socket){
socket.on('chat', function(msg){
io.emit('chat',msg);
});
});
io.on('disconnect', function() {
console.log('user is disconnected');
});
or, a bit simpler version:
var express = require('express');
var app = express();
var server = app.listen(80);
var io = require('socket.io')(server);
io.on('connection', function(socket){
socket.on('chat', function(msg){
io.emit('chat',msg);
});
});
io.on('disconnect', function() {
console.log('user is disconnected');
});

How to connect socket.io in ExpressJS

Good morning why this code don't work (server respond but "io.on" no):
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//this code not work in console // even it can not see
io.on('connection', function(socket) {
console.log('a user connect');
socket.on('disconnect', function() {
console.log('a user disconnect');
});
});
//server respond
http.listen(3000, function() {
console.log('Server running at 3000');
});
I'm a beginner, express and socket.io i downloaded by npm
You need to have a server as well as a client which will make a connection to the socket io server.
Based on what I understood from your code, I guess you want to do something like this:
First your server.js:
#!/usr/bin/env node
var http = require('http');
var express = require('express');
var app = express();
var io = require('socket.io');
var port = 3000;
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
var socketio = io.listen(server);
setInterval(function() {
console.log("Emitting..");
socketio.emit('data', {"x":"123"});
}, 2000);
module.exports = app;
Then your client.html: (I am writing a client, which will log into browser console.)
<html>
<head></head>
<body>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
var socket = io.connect("http://localhost:3000/");
socket.on('connect', function() {
console.log('connected');
});
socket.on('disconnect', function(){
console.log('disconnected');
});
socket.on("data", function(data) {
console.log(data);
});
</script>
</body>
That's it. Now start your server with node server.js and then, open up client.html in your browser and check console. Let me know, if there's anything unclear.

Socket.io with 2 local machines?

I'm trying to use socket.io to send raspberry pi events from my express server. I can't seem to get my raspberry pi to connect to the server though. Any idea on what I'm doing wrong?
Rasberry Pi app:
var socket = require('socket.io-client')('192.168.0.10:3000');
console.log('running');
socket.on('connect', function(){
console.log('connected');
});
socket.on('event', function(data){
console.log(data);
});
socket.on('disconnect', function(){
console.log('disconnected');
});
Express Server:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.emit('event', 'hello');
io.on('connection', function(socket){
console.log('a user connected');
});
io.on('disconnect', function(socket){
console.log('a user disconnected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Needed to change the first line of my Raspberry Pi to include http://
From :
var socket = require('socket.io-client')('192.168.0.10:3000');
TO:
var socket = require('socket.io-client')('http://192.168.0.10:3000');

socket node.js express 404 error

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)

Resources