I cannot get socket.io in node.js to work. When I start my server on port 3000 it never receives a connection even occurrence when I visit http://localhost:3000. I even tried copy and pasting the example code from the socket.io npmjs webpage.
Backend
var io = require('socket.io');
var express = require('express');
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
app.use(express.static( __dirname + '/public'));
server.listen(3000);
io.sockets.on('connection', function (socket) {
console.log('connection established');
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log("Server started on port 3000");
index.html
var socket = io.connect('http://localhost:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
I am using socket.io v0.9.16 and express v3.4
I have extensively look on stack as well as the socket.io docs and can't figure out what is going on. This is killing me!
Note: There are plenty of "socket.io not working" questions on stack but I haven't found any that apply to my problem. The example I provided is almost literally the one from the socket.io npm reference.
Related
I'm following a YouTube tutorial on NodeJs, and for some reason my console.log commands unexpectedly stopped working. Here is the sample code from the tutorial as I've typed it:
const http = require('http');
const server = http.createServer();
server.on('connection', (socket) => {
console.log('New connection')
});
server.listen(3000);
console.log('Listening on port 3000...');
After saving I try using node and nodejs
node app.js
nodejs app.js
but they print nothing.
I have a platform running with Apache + PHP which I distribute to several people through subdomains, Ex: platform.subdomain1.com, platform.subdomain2.com,etc..
. And I would like one of the features of this platform to be Video streaming and i chose to do this with Node.js + socket.io. I don't have much Node experience but I managed to make streaming itself work. I basically have a directory called stream with app.js, index.html and two html files: one to stream the video and one to view.
My problem:
I would like to merge the two so that I can link to these streaming and viewing pages so that each user with their subdomain has their own streaming.
I wonder if there is any way to do it and what it would be.
I could create a directory with the all node streaming files inside each subdomain and create a new instance for each one, like this:
var app = new express();
const http = require("http").Server(app)
http.listen('platform.subdomain1.com',3000);
So that I could link my platform to the address: platform.subdomain1.com/stream:3000
but I'm not sure if it is right to do this or if there is another way to do it. If anyone can help me thank you very much!
My App.js
var express = require("express");
var app = new express();
const http = require("http").Server(app)
var io = require("socket.io")(http);
app.use(express.static(__dirname + "/public"));
app.get('/', function(req,res){
res.redirect('index.html');
});
io.on('connection', function(socket){
socket.on('stream', function(image){
socket.broadcast.emit('stream', image);
});
});
http.listen(3000);
yes, this is the right way to work with socket.io and express together
create express server
bind express server to socket.io
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
// WARNING: app.listen(80) will NOT work here!
app.get('/ping', function (req, res) {
res.send("pong")
});
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);
});
});
Advantage with express:
You will have support for ping/pong or health check in case of AWS load balancer health check or any platform that route request base on target health as socket.io does not support health check in AWS ALB.
you check official documentation suggested by socket.io.
socket.io-Using-with-Express
I am trying to follow tutorial on how to use socket.io with express.js framework and node.js.
Every tutorial I am following suggested I use the following lines to establish a connection in app.js
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3000);
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
});
})
This worked if I use port other than 3000 which I am having to run my application on http://localhost:3000/. I get the error that Port 3000 already in use.
After debugging and looking at the code I think I have an idea of why is this happening. In ./bin/www.js file (created automatically by express js) we have the following lines:
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
but I am not sure how to reuse this created server with same port in my app.js. I am totally new to node.js. How do I setup socket io on the express framework the right way?
hello there please put your server.listen after socket connection like this
var app=require('expess')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
});
})
server.listen(3000);
I hope this would work.Thanks
In order to kill any existing node process, you can run killall node command in your shell.
Remove this line from your code, which is hardcoding the port number -
server.listen(3000);
and add something like this instead -
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'));
So your code would look something like -
var server = require('http').createServer(app);
var io = require('socket.io')(server);
// server.listen(3000);
app.set('port', process.env.PORT || 3000);
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
console.log(data);
});
});
server.listen(app.get('port'));
Now when your run your app again, it would boot on port 3000 by default or you can pass an environment variable (PORT) while starting the server like this to run on other ports.
$ PORT=8080 node app.js
First of all I know a similar question has been asked here, however, I did not understand the answer and my question is worded slightly differently so I am wondering if I can get a more clear answer.
The Express Generator starts an app like so:
app.js
var express = require('express');
var routes = require('./routes/index');
var users = require('./routes/users');
var io = require('socket.io');
(etc.)
I want to use Socket.io so I also install it and require it and following the Socket Docs do this
var server = express.createServer();
var io = require('socket.io')(server);
However, all of my routes, where I really want to handle socket.io events, take place on my routes/index.js page. How do I use socket.io with my router, which is on a different page? How do I pass it in when my server instance was started on app.js (a different page)?
Furthermore, the socket.io docs list two ways to get started with express. The first example shows:
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);
});
});
And the second one shows:
var app = require('express').createServer();
var io = require('socket.io')(app);
app.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);
});
});
Can anyone explain the difference between using the "Express Framework" and "With Express 3 /4" as the documentation differentiates. I would also love to know why the examples use express.createServer() (when a generated express app does not) and why the other example uses both the express and http node modules. Thank you
A working piece of code would be as given below. The important thing here is do all the work with io initialization and io handling once you start listenting on express server. Hope this helps. :-)
var app = require('express')();
var socket_io = require('socket.io');
app.io = socket_io();
// setup app routes and express settings starts ...
// ....
// ... setup app routes and express settings ends
var server = app.listen(app.get('port') || 3000);
app.io.attach(server);
Any working example, using the latest version of nodejs, will work; ideally, it's as simple as possible.
to use create a folder, npm install express socket.io then place in the three files, and 'node app.js'.
layout.jade
!!! 5
title=title
body!=body
index.jade
script(src='http://cdn.socket.io/stable/socket.io.js')
script
//create socket
var socket = new io.Socket();
//connect socket
socket.connect();
//on data recieved
socket.on('message', function(data){
//log data
console.log( data );
//modify data
data.modified = true;
//return data
socket.send(data);
});
app.js
// expressjs.com, a web framework
var express = require('express');
// socket.io, real time communications
var io = require('socket.io');
//create web server
var app = module.exports = express.createServer();
//configure web server
app.configure( function () {
app.set('views', __dirname);
app.set('view engine', 'jade');
app.use(app.router);
});
//handle requests for /
app.get('/', function (req, res, next) {
res.render('index', {
title: 'socket.io test'
});
});
// listen on port 8080
app.listen( 8080 );
console.log("Express server listening on port %d", app.address().port);
// attach socket.io to the web server
var socket = io.listen( app );
// when a client connects
socket.on('connection', function(client){
//send the client some data
client.send({ data: [1,2,3] });
// when the client sends back data
client.on('message', function (msg){
// log the data
console.log( msg );
});
});
Maybe, nowjs is that you want. It's provide simple API to call server functions from client.