I have a node app which routes the request based on the url using restfy.
Now in want to bring socket into picture. But app doesnot redirect to the function when when socket.io is used. I am using FlexSocket.IO library in flex.
Here's my code snippet.
// In app.js
var restify = require('restify')
, http = require('http')
,socket = require('./routes/socket');
var app = restify.createServer();
app.get('/', socket.handle);
app.post('/', socket.handle);
var io = require('socket.io').listen(app);
app.listen(8080, function() {
console.log('%s listening at %s', app.name, app.url);
});
io.configure(function() {
io.set('transports', ['websocket','flashsocket']);
io.set('flash policy port', 843);
});
exports.io = io;
//In socket.js
exports.handle = function (req, res, next) {
console.log('In Handle'); //doesn't print this
io.sockets.on('connection', function(client){
console.log('Connection establiished');
});
In Flex
private var socket:FlashSocket;
socket = new FlashSocket("localhost:8080");
socket.addEventListener(FlashSocketEvent.CONNECT, onConnect);
socket.addEventListener(FlashSocketEvent.MESSAGE, onMessage);
protected function onConnect(event:FlashSocketEvent):void
{
Alert.show('connect'); //This alert is shown.
}
Is there anything wrong with the code? Why is the socket.handle function not called in node?
In app.js, try
var io = require('socket.io').listen(app.server); //app.server instead of just app
Related
Lets say I have the following NodeJS file:
var https = require("https");
var express = require("express");
var app = express();
var options = {};
var serverPort = 8443;
var server = https.createServer(options, app);
var io = require('socket.io')(server);
var numUsers = 0;
app.get('/', function(req, res){
res.sendFile('/home/domain/index.php');
});
io.on('connection', function(socket){
socket.on('user-login', function(data){
++numUsers;
});
socket.on('new message', function (msg,room) {
console.log(msg);
});
socket.on("disconnect", function() {
--numUsers;
});
});
server.listen(serverPort, function(){
console.log("\n--------------------------------");
console.log('Node HTTPs Server');
console.log('Currently Listening on port %d',serverPort);
console.log("--------------------------------");
});
Since I can't get SNI to work on my server, I'll have to go the old fashioned way and write a script for each subdomain. But what I'd like to do is have the functions inside of the io.on('connection', function(socket) {} area to be included. So not included like a class or anything like that, but literally the code is just taken from another file and processed as if it were in that file already. A lot like PHP does includes. Is this possible?
Simplest solution would be to read code using fs.readFile[Sync] and pass it to eval inside io.on('connection', function(socket) {})
io.on('connection', function(socket){
socket.on('user-login', function(data){
++numUsers;
});
socket.on('new message', function (msg,room) {
console.log(msg);
});
socket.on("disconnect", function() {
--numUsers;
});
// eval function loaded outside io.on('connection')
eval(someFunctionBody);
// or
eval(fs.readFileSync('path/to/function/body.js'));
});
Can't you just use require?
functions.js
function myFunc() {
console.log("I am a funky func");
}
module.exports = {
myFunc,
myOtherFunc,
};
index.js
var https = require("https");
var express = require("express");
// snip
var funcs = require('./functions');
io.on('connection', function(socket){
// snip
funcs.myFunc();
});
I'm creating a simple NodeJS/Express webapp that has an api and sockets. Basically, what I want to do is send data to my API from an external source, and then send this data to the socket in pageView/id. Right now it works, but it is sending data to all of the views, not the specific pageView/id.
I know I have to create Rooms and I tried doing this by having my sockets join the room when the webapp is navigated to page/id: (note I do not have logged in users)
router.get('/:id', function (req, res, next) {
res.io.on('connection', function(socket) {
console.log("user connected");
//join room here
});
});
But then this creates multiple connections, every time I refresh the pageView I get a new connection + previous connections on my server side.
How can I join the room when a pageView/id is accessed? Here is my setup...
app.js
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(function(req, res, next){
res.io = io;
next();
});
module.exports = {app: app, server: server};
/bin/www
var app = require('../app').app;
var http = require('http');
var server = require('../app').server;
server.listen(port);
pageView.hbs
var socket = io.connect();
socket.on('mySocket', function (data) {
console.log(data);
});
pageView.js
router.post('/updatePage', function (req, res, next){
//send to the view
res.io.emit("mySocket", {
device: device
});
});
Ok, I understand now, your mistake is to create the listeners inside your route. You need only one set of event listeners on io. So, this would work :
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(socket) {
console.log("user connected");
socket.join(room);
});
module.exports = {app: app, server: server};
remember that you can pass parameters on your connection, for example, you can do :
var myroom = window.location.pathname.split('pageView/')[1]; //example to get room name, be creative !
var socket = io.connect("http://127.0.0.1:3000/", { query: 'room='+myroom+' });
and on the server :
io.on('connection', function(socket) {
console.log("user connected");
var room = socket.handshake.query.room; // === 'myRoom'
socket.join(room);
socket.emit("mySocket", {
device: device
});
});
pageView.js
router.post('/updatePage', function (req, res, next){
});
I'm a little bit confused;
I would like to use socketIO on NodeJS app.
I've created this (pseudo)code:
//server.js
var app = express();
//some code...
var router = require('./app/router');
app.use(router);
var server = app.listen(appConfig.app.port, function () {
var port = server.address().port;
});
var io = require('socket.io')(server);
io.on('connection', function (client) {
console.log('Client connected...');
client.on('join', function (data) {
console.log(data);
});
});
//client.js
var socket = io.connect('http://localhost:5555');
socket.on('connect', function(data) {
socket.emit('join', 'Hello World from client');
});
Everything is fine. But !
At now, I would like to emit event in another file.
I have router and POST request. I want to emit event on POST request (request handler is in another file).
//router.js
router.route("/addmenu").post(function (req, res) {
menuModel.addMenu(req.body,function(data){
//I WANT EMIT HERE
res.json(data)
});
};
);
I have to initialize router before start server, but I have to pass server to IO... How pass IO to router ?
You can try this
//server.js
var app = express();
//some code...
var io;
var getIOInstance = function(){
return io;
};
var router = require('./app/router')(getIOInstance);
app.use(router);
var server = app.listen(appConfig.app.port, function () {
var port = server.address().port;
});
io = require('socket.io')(server);
io.on('connection', function (client) {
console.log('Client connected...');
client.on('join', function (data) {
console.log(data);
});
});
//router.js
module.exports = function(getIOInstance){
router.route("/addmenu").post(function (req, res) {
menuModel.addMenu(req.body,function(data){
//I WANT EMIT HERE
getIOInstance().sockets.emit(...)
res.json(data)
});
};
return router;
);
This solution will work if you want to 'notify' all connected clients.
If you need to notify only a specific client, then I will advise you to use an event-emitter module in order to communicate these events and not share your socket instances across multiple files.
In router.js you can do something like:
//router.js
module.exports = function(io) {
var router = //What you declared it to be
router.route("/addmenu").post(function (req, res) {
menuModel.addMenu(req.body,function(data){
//I WANT EMIT HERE
res.json(data)
});
};
);
return router;
}
//server.js
//Change this line to be like the one below
var router = require('./app/router');
//.........
//.......
//Desired way
var router = require('./app/router')(io);
The answer of #jahnestacado does the job, but in case you already have an existing code base, then you need to change the structure of each file, where you might need the socket.io object, to pass it in as an argument.
A better way to do it then, would be:
To create the getIO() function—just as #jahnestacado did—, where you instantiate the io object (inside server.js), and export it.
var io;
exports.getIO = () => io;
Then require it wherever you need it. But make sure to execute the function only when you need it. Typically inside your controller function:
const getIO = require('../server').getIO;
exports.updateSAE = (req, res) => {
let io = getIO();
io.emit();
// rest of your controller function code
}
Note that I did not call the getIO function outside the controller function. For example, the following would probably not work:
const getIO = require('../server').getIO;
var io = getIO();
exports.updateSAE = (req, res) => {
io.emit();
// rest of your controller function code
}
Simply because the socket.io object could have not been initialized when you call the function, thus returning undefined.
I've been trying to figure out why I can't get any emits to show up in my terminal and it seems that everything is running fine.... except for seeing the emits. Here is my code
var express = require('express');
var path = require('path');
// Create a new Express application
var app = express();
var views = path.join(process.cwd(), 'views');
app.use("/static", express.static("public"));
// Create an http server with Node's HTTP module.
// Pass it the Express application, and listen on port 3000.
var server = require('http').createServer(app).listen(3000, function() {
console.log('listening on port ' + 3000)
});
// Instantiate Socket.IO hand have it listen on the Express/HTTP server
var io = require('socket.io')(server);
var game = require('./game');
app.get('/', function(req,res) {
res.sendFile(path.join(views, 'index.html'));
});
io.on('connect', function(socket) {
io.emit('connection', { message: "You are connected!" });
game.initGame(io, socket);
socket.emit('connected', { message: "You are connected!" });
io.sockets.emit('test', 'test')
});
Any help would be great!
Emits are not automatically printed. socket.emit will send a message back to the client, not to the terminal. Use console.log("whatever") to print to the terminal:
io.on('connect', function(socket) {
console.log('Client connected');
socket.on('test', function(data) {
console.log("Got message of type 'test'containing data:", data);
});
});
I wrote a very simple demo about socket.io And I package it by using phonegap. I found there is problem. After I open my app about ten seconds ,the connection will disconnect because of xhr poll error.if I refresh the page in disconnect event the error won't come again.
I use 1.2.0 version.here is my code. I already simplify it.
server:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
io.sockets.on('connection', function (socket) {
console.log("disconnect--"+socket.id+"--"+io.sockets.server.eio.clientsCount);
socket.on('disconnect', function () {
console.log("disconnect--"+io.sockets.server.eio.clientsCount);
});
});
http.listen(80, function () {
console.log("server statrt");
});
client:
$(document).ready(function () {
var socket = io("http://192.168.0.106:80");
socket.on('connect', function () {
alert("connect");
});
socket.on('error', function (data) {
alert(data);
});
socket.on('disconnect', function () {
alert("disconnect");
});
socket.on("reconnect", function () {
alert("reconnect");
})
});
thanks for help.my English is not very good
You have to open the socket.io connection when the deviceready event is fired.
document.addEventListener('deviceready', function() {
var socket = io("http://192.168.0.106:80");
socket.on('connect', function() {
alert("connect");
});
socket.on('error', function (data) {
alert(data);
});
socket.on('disconnect', function () {
alert("disconnect");
});
socket.on("reconnect", function () {
alert("reconnect");
});
});
Socket.io example
For those of you using Google Chrome, FYI Chrome does not fire 'deviceready'. Instead you should use 'DOMContentLoaded'.