Hi i am working with socket.io and node.js (express.js).
I saw a lot of examples, coding from the app.js file. Now i would like to organize better the code, i would like to have all my sockets.io handlers in other modules/files.
This is what i have now:
app.js
var moduleExports = require('./routes/moduleExports');
app.get('/', function(req, res){
res.sendfile(path.join(__dirname + '/index.html'));
io.on('connection', function(socket){
moduleExports.socketio(io, socket);
});
});
moduleExports.js
module.exports = {
init: function(){
//some other functions
}, socketio: function(io, socket){
socket.emit('chat', 'Wellcome!');
socket.on('chat', function (data) {
socket.broadcast.emit('chat', data);
//socket.broadcast.emit('chat', data);
});
socket.on('disconnect', function () {
socket.broadcast.emit('chat', 'Disconnected');
//socket.broadcast.emit('chat', data);
});
}
};
PROBLEM:
If I open 2 different browsers, one browser is for John and one browser is for Doe.
If John sends a message, Doe recieve it two times. If I open again a third browser, and send a message from John, Doe and the third browser are reciving three times the message.
I dont know exactly why this happen. I know something is happen with the "moduleExports.js" file. But I need a way to code my socket.io handlers outside of app.js. So i thought this would be a good pattern but it isnt.
By the way, the client code (it works):
<script src="/socket.io/socket.io.js"></script>
var socket = io();
function submit(){
socket.emit('chat', $('#m').val());
$('#m').val('');
return false;
}
The io.on('connection' you should have it just once in your code, everytime a client connects to your server that event will be thrown, if you have it multiple times I guess it runs more times. What I would do is the the following:
var moduleExports = require('./routes/moduleExports');
io.on('connection', function(socket){
moduleExports.socketio(io, socket);
});
app.get('/', function(req, res){
res.sendfile(path.join(__dirname + '/index.html'));
});
Related
I have create a socket in app.js
APP.JS
var app = express();
var server = require('http').createServer(app)
var io = require('socket.io').listen(server);
app.set('socketio', io);
io.sockets.on('connection', function(socket){
console.log('Connesso');
socket.on('message', function(data){
console.log("Oo");
})
})
In my html page I have a js script
newex.onsubmit = function(event){
event.preventDefault();
socket.emit('message', {
name: document.getElementById('name').value,
desc: document.getElementById('description').value
});
}
So, when an user submit a form, the socket should send a "signal", but I want catch the signal in a routing page, not in my app.js
I tried with:
ROUTING PAGE
io = req.app.get('socketio');
io.on('message', function(message){
console.log(message);
})
But it doesn't work! I get that I need to put io.on(...) into io.sockets.on clousure but I don't get why. Can you explain me mechanism of socket.io?
EDIT
I set 'socket' in this way and I try code of tbking but it doesn't work anyway
io.sockets.on('connection', function(socket){
console.log('Connesso');
app.set('socket', socket);
//socket.on('message', function(message){console.log("Ricevuto")})
})
You need to listen to the messages from the specific socket the client is connected to.
Try this in your routing file:
var socket = req._socket;
socket.on('message', function(message){
console.log(message);
})
I'm testing socket.io and do a simple chat message (following the tutorial in the official website)
I opened 2 windows:
When I emit the event in the first window opened, its ok.
But, when I emit the event in second window this send the event 2 times(duplicated).
PS: if I open a third window, this send the event 3 times
Node.js code:
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
io.on('connection', function(socket) {
socket.on('chatMessage', function(msg){
io.emit('chatMessage', msg);
});
});
});
Client side:
methods: {
sendMessage: function () {
socket.emit('chatMessage', this.text);
}
socket.on('chatMessage', function(msg){
console.log('Client side message: ' + msg)
vmIndex.messages.push(msg);
});
It's because you're creating a connection listener every time someone visits the '/' route. Try moving the socket-io code outside of the '/' route function.
As Eric mentioned, move your connection listener outside of the / route in order to prevent it from getting created every time someone visits the page.
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
io.on('connection', function(socket) {
socket.on('chatMessage', function(msg){
io.emit('chatMessage', msg);
});
});
I am trying to create a simple script to send data from a file every to the client every time the file is updated. I have tested and found that the file is read, but the client doesn't receive anything. there are no errors in the console. I am fairly new to socket.io.
node.js code
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require("fs");
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/popup.html');
});
fs.watchFile("assets/popup.json", {interval:100}, function(curr, prev)
{
fs.readFile("assets/popup.json",{encoding:"utf8"}, function(err, data){
io.emit("popup", data)
})
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
client code
var socket = io();
socket.on('popup', function(msg){
alert("hello")
});
Whenever things aren't working right like this, you need to resort to "debug mode". In that mode, you need to gather all possible events that might be happening and see what you learn from that. To that end, add this code to the client:
var socket = io();
socket.on('popup', function(msg){
console.log("hello: ", msg)
});
socket.on('connection', function() {
console.log("client connected");
});
socket.on('connect_error', function(err) {
console.log("client connect_error: ", err);
});
socket.on('connect_timeout', function(err) {
console.log("client connect_timeout: ", err);
});
These messages are all documented in the client-side doc on the socket.io Github site which you can find by Googling "socket.io github" at any time.
Then, see what you see in the browser console when the page loads. If you don't know how to open the browser console in whichever browser you are using, google it to find out. You need to be looking at the debug console when the page loads.
FYI, we're assuming that you've loaded socket.io into the page via a script tag before this code. If not, that error will show in the console too.
The OP then gets this error:
client connect_error:
Error: server error at Socket.onPacket (socket.io-1.2.0.js:1)
at XHR.<anonymous> (socket.io-1.2.0.js:1)
at XHR.Emitter.emit (socket.io-1.2.0.js:1)
at XHR.Transport.onPacket (socket.io-1.2.0.js:1)
at callback (socket.io-1.2.0.js:2)
at Object.exports.decodePayload (socket.io-1.2.0.js:2)
at XHR.Polling.onData (socket.io-1.2.0.js:2)
at Request.<anonymous> (socket.io-1.2.0.js:2)
at Request.Emitter.emit (socket.io-1.2.0.js:1)
at Request.onData (socket.io-1.2.0.js:2)
OK, progress. How are you loading socket.io in the client page? This seems like it might be that you have mismatched versions of socket.io in client and server. You should be doing:
<script src="/socket.io/socket.io.js"></script>
and then your server will be feeding the client page the exact same version of socket.io. Also, since this error reports client-side socket.io 1.2.0, what version of socket.io is installed on the server?
try this
socket.on('popup', function(msg){
socket.emit('message',"popup");
});
The issue appears to be you don't actually connect to a local socket.io server. By running node server.js with the code below you can start a web server. Then navigate to localhost in your browser to see the changes in console made to popup.json.
server.js
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
fs.watchFile("popup.json", {interval: 100}, function (curr, prev) {
fs.readFile("popup.json", {encoding: "utf8"}, function (err, data) {
io.emit("popup", JSON.parse(data));
})
});
index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost');
socket.on('popup', function (data) {
console.log(data);
});
</script>
I started learn socket.io and use this example of chat.
When I go to ip:8080/public/index.html, I also need access to other files, for example other JS scripts, which will be used on client side in the browser. But when I put script load like this:
<script src="/js/phaser.js" type="text/javascript"></script>
the web server does not return it, and I need it on this handler code.
I have this code:
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(8080);
function handler (req, res) {
console.log(req.headers.referer);
fs.readFile(__dirname + '/public/index.html', // <--- I need here put filename which client wants it, but when I console.log to req it return HUGE data, I not found anythink usefull
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
socket.broadcast.emit('new message', data);
console.log(data);
});
socket.on('msg', function(data){
console.log(data);
})
});
You can use Express static for serving static files like your *.js files.
I have an express node.js server serving Socket.io. I would like the ability to make get requests to the express server that will automatically send a message to a channel.
var app = require('express').createServer()
, io = require('socket.io').listen(app)
app.listen(80);
app.get('/:channel/:message', function (req, res) {
//Code to create socket
socket.emit("sent from get", {channel:req.params.channel, message:req.params.message})
});
io.sockets.on('connection', function (socket) {
socket.on('sent from get', function (data) {
socket.broadcast.to(data.channel).emit('channel message', { message: data.message});
});
});
How to I create (and destroy) a socket connection in the app.get block?
(For clarity, I want to use this to send a quick message from a rails server when a particular object is saved, and have a message pushed to each appropriate user.)
io.sockets.in(req.params.channel).emit("channel message", {mes:req.params.message})
That will send a message to all users in the requested channel.
var chat = io.of('/chat').on('connection', function (socket) {
socket.emit('a message', { that: 'only', '/chat': 'will get' });
chat.emit('a message', { everyone: 'in', '/chat': 'will get' }); });
The following example defines a socket that listens on '/chat'