I'm doing an app with express, mongoose, and socket.io. My intention is to make that when a user enters, he joins a room whose name is the name of the city with which he registered. The idea is that when a user A and B belong to the same city, they are linked to the same room . But I can not get it to work for me. When a user A, issues information to the room , this information does not reach user B, nor the others connected to it.
This is my server-side file:
app.js
var express = require('express');
// .. others modules here ..
var app = express();
var server = require("http").Server(app);
var io = require("socket.io")(server);
// my custom routes
var routes = require('./routes/index');
// socket.io config
io.on('connection',function(socket){
console.log("new user connected id socket: "+socket.id);
// evento disparado cuando un usuario inicia sesion
socket.on("join", function(data){
socket.join(data);
console.log("connected to room: "+data);
console.log(io.nsps["/"].adapter);
});
//evento que recibe el nombre de la ciudad a la que emitirán datos
socket.on("notificar",function(ciudad){
console.log("sending data to: "+ciudad);
io.to(ciudad).emit("recibir",{message: "new "+ciudad});
});
});
server.listen(3000, function () {
//console.log('Example app listening on port 3000!');
});
All client-side pages include the socket-client.js script for the connection to the server.
socket-client.js
var socket = io.connect('http://localhost:3000');
socket.on('connection', function (data) {
console.log(data);
});
// para enviar datos a la room
$("#form-report").on('submit',function(i,e){
// city es la ciudad que será el nombre de la room
var city = $("#report-city").html();
socket.emit("notificar",city);
});
// para recibir datos de la room
socket.on('recibir', function(socketData){
alert("recibiendo datos: "+socketData);
});
The file validate-user.js is a file that captures the user and password of the login form, and validates if it exists. If so, then fire the server's join event.
validate-user.js
// usuario y contraseña desdel el formulario
input_email = $("form#form-login input#email");
input_pass = $("form#form-login input#password");
$("#btn-login").on('click',function(i,e){
email = input_email.val();
pass = input_pass.val();
// petición para validar si existe
$.ajax({
type: 'post',
dataType: 'json',
url: 'http://localhost:3000/autenticate/'+email+'/'+pass,
success: function(data){
if(data.exist === false){
$("#label-email").html(data.message);
return true;
input_email.val("");
input_pass.val("");
}else{
// SI EXISTE disparar e vento "join" del servidor
socket.emit("join",data.data.ciudad);
location.href = "http://localhost:3000/"+email;
}
},
error: function(err){
console.log(err);
return false;
}
});
return false;
});
The problem then is that when the two users of the same city are connected, they do not join in the same room. Here I leave some screenshoots when user A was connected and later user B.
screenshoots