I have no idea why this won't work; here's my code:
io.on('connection', function(socket) {
socket.on('join', function(data) {
socket.join(data.email); // We are using room of socket io
User.findById(bid.highestBidder, (err, theUser) => {
io.sockets.in(theUser.email).emit('outbid', {msg: 'You have been outbid!'});
});
});
});
Now, here's the code on the client-side javascript:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var userEmail = $("#userEmail").val();
socket.emit('join', {
email: userEmail
});
socket.on("outbid", function(data) {
console.log(data);
});
</script>
If you need me to paste more code, let me know and I will.
Basically, when the socket function gets executed, it's sending it to every browser instead of just the room with the "email."
io.on('connection', function(socket){
socket.join('room name');
});
io.to('room name').emit('some event');
Related
I'm trying to send a message to everyone except the sender.
server.js
io.on('connection', (socket) => {
socket.on('typing', (msg) => {
io.broadcast.emit('typing', msg);
});
});
cleint.html
var socket = io();
socket.emit('typing', 'some message');
socket.on('typing', (msg) => {
$('#myLabel').append(msg);
});
If i remove the broadcast in server to just io.emit then it works, but the sender also sees the message.
instead of
io.broadcast.emit('typing', msg);
try
socket.broadcast.emit('typing', msg);
I'm starting with socket.io and I can send private messages but How I can send a message to all,
At the following code(its for test purpose) the first user receives a private message every time an user is connected , How I can broadcast all the message to all clients including the sender?
this is the client
var socket = io('http://localhost:3000');
socket.emit('user_join', { username:'{{username}}'});
socket.on('private',function(data){
alert(data);
});
socket.on('message',function(data){
console.log(data);
});
and this is the server
var connectedUsers = []
io.on('connection', (socket) => {
console.log('a user connected');
socket.on('user_join', function(msg){
connectedUsers[msg.username] = socket;
connectedUsers['aaa'].emit('private','private message');
socket.emit('message','public message');
});
console.log(connectedUsers.keys());
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
The documentation is good https://socket.io/docs/emit-cheatsheet/
There's lots of variations, but I think you're looking for:
io.emit('an event sent to all connected clients');
So, I've been trying to write this chat-app for awhile now with Node.js, express,js and socket.io and everything works well. Apart from the fact that I'm trying to include a bot called Steve. Steves purpose is to welcome you to the chat, or to have him join after you've joined and introduce himself. As well as beeing able to reply to some "commands". Hi, How are you etc. Etc.
But that part is somethin that I can't get to work.
I'm not asking of you to write the code for me. But if you could please tell me if I'm on the right track and maybe come with some ideas, tips etc. I'm stuck.
Thanks in advance.
Here's my index.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
users = [];
connections = [];
server.listen(process.env.PORT || 3000);
console.log('Server running...');
app.use(express.static(__dirname + '/public'));
app.get('/public', function(req, res) {
res.sendFile(__dirname + 'index.html')
});
io.sockets.on('connection', function(socket) {
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
// Disconnect
socket.on('disconnect', function(data) {
users.splice(users.indexOf(socket.username), 1);
updateUsernames();
connections.splice(connections.indexOf(socket), 1);
console.log('Disconnected: %s sockets connected', connections.length);
});
// Send Message
socket.on('send message', function(data) {
io.sockets.emit('new message', {msg: data, user: socket.username});
});
<code>
// New User
socket.on('new user', function(data, callback){
callback(true);
socket.username = data;
users.push(socket.username);
updateUsernames();
});
function updateUsernames() {
io.sockets.emit('get users', users);
}
});
And here's my script.js
$(function () {
var socket = io.connect();
var $messageForm = $('#messageForm');
var $message = $('#message');
var $chat = $('#chat');
var $messageArea = $('#messageArea');
var $userFormArea = $('#userFormArea');
var $userForm = $('#userForm');
var $users = $('#users');
var $username = $('#username');
var name = 'Steve', adress = 'localhost:3000', socket;
function join(){
socket.on('new user', name);
socket.emit('message', "Hi! I am Steve. Steve the Chatter!");
socket.on('new message', listener);
};
function listener(data){
if(data.message=='Hi')
socket.emit('message', 'Hi, '+$username+'.');
};
join();
$messageForm.submit(function(e) {
e.preventDefault();
socket.emit('send message', $message.val());
$message.val('');
});
socket.on('new message', function(data) {
$chat.append('<div class="well"><strong>'+data.user+'</strong>: '+data.msg+'</div>');
});
$userForm.submit(function(e) {
e.preventDefault();
socket.emit('new user', $username.val(), function(data) {
if(data) {
$userFormArea.hide();
$messageArea.show();
}
});
$username.val('');
});
socket.on('get users', function(data) {
var html = '';
for(i = 0;i < data.length;i++){
html += '<li class="list-group-item">'+data[i]+'</li>'
;
}
$users.html(html);
});
});
I'm having problems with socket.io.
I try to create a single socket connection where this is connected to multiple rooms.
This is my current code:
function JoinRoom(id){
socket = io(domain);
socket.on('connect', function (data) {
console.log('Connected to ' + room);
socket.emit('room', room);
});
socket.on('message', function (data) {
console.log(data);
});
}
The problem is that if I remove the var socket.io(domain) function was not connected and do not receive data from the room.
Example:
socket = io(domain);
function JoinRoom(id){
socket.on('connect', function (data) {
console.log('Connected to ' + room);
socket.emit('room', room);
});
socket.on('message', function (data) {
console.log(data);
});
}
If I take the socket.io() function of the JoinRoom() function I do not receive messages message or anything. It does not work.
What am I doing wrong? Any solution?
On the client, create an event to indicate the access room.
On the server, apply the access logic.
Client
var socket = io('http://localhost');
function JoinRoom(data){
socket.emit("join", data);
}
var data = {
room:'ejemplo'
};
JoinRoom(data);
Server
var io = require('socket.io')(app);
io.on('connection', function(socket){
socket.on('join', function(data){
console.log(data); // data = { room: String, ...}
socket.join(data.room);
});
socket.on('leave', function(data){
socket.leave(data.room);
});
Heya I'm trying to build a small chat client to learn how websockets work in order to make a game in canvas. It works great with sending sockets but they are only sending it to the the one who wrote it.
I guess I've missed something small, but I can't understand why it won't work.
Server side code
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.on('user-message', function (data) {
console.log(data);
sendMessage(data.message);
});
});
var sendMessage = function(message) {
io.sockets.emit('server-message', {message: message});
}
Client side code
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('server-message', function (data) {
var history = $('#chatbox').val();
$('#chatbox').val(history + "\n" + data.message)
});
$("#write").keyup(function(event){
if(event.keyCode == 13){
socket.emit('user-message', {message: $(this).val()});
$(this).val('');
}
});
</script>
You can use socket.broadcast.emit to send a message to all other sockets.
io.sockets.on('connection', function (socket) {
socket.on('user-message', function (data) {
console.log(data);
sendMessage.call(socket, data.message);
});
});
var sendMessage = function(message) {
this.emit('server-message', {message: message});
this.broadcast.emit('server-message', {message: message});
}