I am relatively new to using node.js and I have been searching for awhile but I haven't been successful in implementing socket.join
I have a chat room which I want to be setup privately between two users, similar to private messaging.
What I wish to have is make dynamic chat rooms between users. The problem I am getting right now is that the messages are still being broadcast to all the clients connected to the server.
Here's what I have on the server
server.js
io.sockets.on( 'connection', function( socket ) {
var unique_room = "";
socket.on('join', function(room) {
socket.join(room);
unique_room = room;
console.log("User Joined the room: "+room);
});
socket.on('message', function (data) {
io.sockets.in(unique_room).emit('message', data.message);
});
});
server.listen(1337);
client.js
var socket = io.connect( 'http://localhost:1337' );
socket.on('connect', function() {
socket.emit('join', 'dddsdjkfh1123'); //chat room id unique to two users
});
socket.on( 'message', function( data ) {
$("#chat" ).html(data.user + ":" + data.message);
});
//Form js
function sendpm(room,message,name) {
socket.emit( 'message', { room: room, message: message, name: name });
}
Please let me know what I am doing wrong. Any help would be greatly appreciated.
Thank you so much.
You should emit some the room id from the client when you send the message, and get that room id to send message to only that client in that room.
Try doing this in your server.js:
io.sockets.on( 'connection', function( socket ) {
socket.on('join', function(room) {
socket.join(room);
socket.room = room;
console.log("User Joined the room: "+socket.room);
});
socket.on('message', function (data) {
io.sockets.in(data.room).emit('message', data.message);
});
});
Related
I am using socket.io in nodejs for chat system, and I am able to send the message on server in socket.js file in nodejs. But when I emit that message to other users from socket in nodejs. Then this message is not receiving on the client side.
Here is my nodejs code:
var users = [];
const io = require('socket.io')(server, {
pingInterval: 5000,
pingTimeout: 5000
});
io.on('connection', (socket) => {
console.log("User connected: ", socket.id);
socket.on("user_connected", function (id) {
users[id] = socket.id;
io.emit("user_connected", id);
});
socket.on('message', (msg) => {
var socketId = users[msg.friendId];
socket.to(socketId).emit('new_message', msg);
let chat = new ChatData({
user: msg.userId,
friend: msg.friendId,
message: msg.message
});
chat.save().then(() => {
console.log('message saved');
}).catch((err) => {
console.log('error is ', err);
});
});
});
Here is my client side code:
const socket = io("ws://localhost:3000");
socket.on('connect', function() {
socket.emit("user_connected", userId);
});
function sendMessage(message) {
let msg = {
userId: userId,
friendId: friendId,
message: message.trim()
}
// Send to server
socket.emit('message', msg);
}
// Recieve messages
socket.on('new_message', (msg) => {
console.log(msg)
});
Client side socket js library https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.js
I have a problem with socket.io from nodeJs.
I am trying to create a connection from a client to a server. Once the client is connected, the server sends a message to the client (ack).
Until then, everything works fine but when I disconnect the server and restart it, it sends me the message twice to the client.
If I repeat the manipulation a third time, three messages will appear.
I have captured the problem:
client.js
var socket = require('socket.io-client')('http://localhost:8050', {
'forceNew':true
});
socket.on('connect', onConnect);
function onConnect(){
console.log('connect ' + socket.id);
socket.emit('sendConnect','First connect');
socket.on("ack", function(data) {
console.log("ack reçu");
});
socket.on('disconnect', function(reason) {
console.log(reason);
});
}
server.js
var io = require("socket.io");
var sockets = io.listen(8050);
sockets.on('connection', function (socket) {
socket.on('sendConnect', function (data) {
console.log("message :" + data);
socket.emit('ack');
socket.on('disconnect', function() {
console.log('Got disconnect!');
});
});
});
I looked if this bug was already arriving without finding an answer.
I must surely be doing something wrong!
Thank you in advance for your help.
Your onConnect function adds new event listeners each time the socket connects. Move the event subscriptions out of onConnect like this:
var socket = require('socket.io-client')('http://localhost:8050', {
forceNew: true
});
socket.on('connect', onConnect);
socket.on('ack', function(data) {
console.log('ack reçu');
});
socket.on('disconnect', function(reason) {
console.log(reason);
});
function onConnect() {
console.log('connect ' + socket.id);
socket.emit('sendConnect', 'First connect');
}
I'm having a problem sending data to a specific client.
Serverside
var io = require('socket.io').listen(app.listen(app.get('port')));
io.sockets.on('connection', function (socket) {
socket.on('onHabboConnection', function (data) {
socket.id = data.id;
socket.join(data.id);
io.sockets.in(data.id).emit('onHabboConnected');
});
socket.on('onHabboConnected', function (data) {
console.log("<" + socket.id + "> " + data.username + " has joined the game.");
});
});
Clientside
var socket = io.connect('http://localhost:3000');
$( document ).ready(function() {
socket.emit('onHabboConnection', { id:'<%= user.id %>' });
});
socket.on('onHabboConnected', function () {
console.log("WORKING" );
socket.emit('onHabboConnected', { username: '<%= user.username %>'});
});
io.sockets.in(data.id).emit('onHabboConnected');
I've attempted all over methods but they all throw errors.
This one doesn't throw an error but doesn't do anything!
You are overwriting the unique identifier socket.id that was generated on the client side. That's why you are emitting but it's not going anywhere since that client doesn't exist:
socket.on('onHabboConnection', function (data) {
//socket.id = data.id; remove this
socket.join(data.id);
io.sockets.in(data.id).emit('onHabboConnected');
});
If you want to change socket.id , then change it on client side.
Following is the code on front end, where storeSelUserId contains user_id to send the message-
FYI - Node Version 1.1.0
// Socket Notification
var socket = io('http://localhost:6868');
socket.on('connection', function (data) {
socket.emit('send notification', { sent_to: storeSelUserId });
});
Following is the server code in routes file -
var clients = {};
io.on('connection', function (socket) {
socket.emit('connection', "Connection Created.");
socket.on('send notification', function (sent_to) {
console.log(sent_to);
});
});
In console sent_to is showing the array of user_id.
Now being a starter in socket.io I stuck with the solution that how do I send the message to these particular userids.
I search and found that I need to push each user with its sockets so I reformed it to -
var users = [];
io.on('connection', function (socket) {
users.push({socket_id: socket.id});
socket.emit('connection', "Connection Created.");
socket.on('send notification', function (sent_to) {
console.log(sent_to);
});
});
But I am in dilemma that else do I need to do to store which user_id refers to which socket_id and then update the div of users with that particular ids?
EDIT -
Add Controller - (Front End)
Front end Interface where memo is created and send to particular users
var socket = io('http://localhost:6868');
socket.on('connection', function (data) {
socket.emit('send memo notification', {creator_id: creator_id, sent_to: [Array of user_ids to whom memo to send]});
});
Dashboard controller - (Front End)
Front end Interface where notification count to show "notificationCount"
if (SessionService.currentUser._id) {
var socket = io('http://localhost:6868');
socket.on('connection', function (data) {
socket.emit('get notifications', {user_id: SessionService.currentUser._id});
});
socket.on('notification data', function(data){
console.log("-- Not Data Test -");
$scope.notificationCount = data.length;
});
}
Code at server end -
io.on('connection', function (socket) {
socket.emit('connection', "Connection Created.");
socket.on('send memo notification', function(data) {
notifications.createNotification(data);
});
socket.on('get notifications', function(data){
notifications.getNotifications(data, function(response){
socket.emit('notification data', response.data);
});
});
});
Backend controller code -
exports.getNotifications = function(data, callback) {
var userId = data.user_id;
Notification.find({receiver_id: userId}, function(err, response){
if (err)
callback({"message": "error", "data": err, "status_code": "500"});
else
callback({"message": "success", "data": response, "status_code": "200"});
});
};
exports.createNotification = function(data) {
var notificationData = data;
var x = 0;
for(var i=0; i< notificationData.length; i++) {
// Code
Notification(notificationData[i]).save(function(err,response){
if (err)
return false;
});
if (x === notificationData.length - 1) {
return true;
}
x++;
}
};
If you want to use your own user ids then there is no way around mapping the socket id to the user id. I assume a client knows its user id from somewhere, so it could send its user id to the server after connection.
Client
socket.on('connection', function (data) {
socket.emit('setUserId', myUserId);
});
The server saves the socket for each user id.
socket.on('setUserId', function (userId) {
users[userId]=socket;
});
If you have such a mapping in the server you can send a message just to this client using the user id.
socket.on('send notification', function (userId) {
users[userId].emit('notification', "important notification message");
});
Edit: Saving the corresponding socket directly is even better.
According to what i understand, you need private notification send to only some users. For that, save your users name to whom you want to send and their corresponding socket in different hashes.
username [socket.name] = username to be added;
usersocket [ socket.name ] =socket;
Then to emit the messages to that user only, use
usersocket[ socket.name ].emit('event for send message', ' what you want to send ');
I'm sending data to all clients but I need to send the data only one client (who make request) too.
app.post(.....){
myModel.save(function (err) {
if (err) return handleError(err);
///send to all
io.sockets.emit("ev", { ...... });
//// send to one client
......
});
}
There is a function called io.sockets.emit but there is no io.socket.emit.
I assume that in the post method you have identified the user or session.
So you can create a room per user to later emit on it.
client.js
var room = "#usernameRoom";
socket.on('connect', function() {
socket.emit('privateroom', room);
});
socket.on('privatemessage', function(data) {
console.log('Incoming private message:', data);
});
server.js
io.sockets.on('connection', function(socket) {
var socket_room;
socket.on('privateroom', function(room) {
socket_room = room;
socket.join(room);
});
socket.on('disconnect', function() {
if (socket_room) {
socket.leave(socket_room);
}
});
});
app.post(.....){
myModel.save(function (err) {
if (err) return handleError(err);
///send to all
io.sockets.emit("ev", { ...... });
//// send to one client
// now, it's easy to send a message to just the clients in a given private room
privateRoom = "#usernameRoom";
io.sockets.in(privateRoom ).emit('privatemessage', 'Never reveal your identity!');
});
}
hope that helps