Send Response to All Clients Except Sender

Send response to all clients except sender

Here is my list (updated for 1.0):

// sending to sender-client only
socket.emit('message', "this is a test");

// sending to all clients, include sender
io.emit('message', "this is a test");

// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");

// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');

// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');

// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');

// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');

// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');

// list socketid
for (var socketid in io.sockets.sockets) {}
OR
Object.keys(io.sockets.sockets).forEach((socketid) => {});

Is there any way to send a message to everyone except the sender?

Inserting them into a list can help. For example...

For the server side:

import socket
import threading

# This is where you store all of your Client IP's
client_list = []

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "yourip"
server_port = 8888
server.bind((server_ip, server_port))

def check_client(client_ip):
while True:
data = client_ip.recv(1024).decode()

if "condition" in data:
for ip in client_list:
if ip != client_ip:
ip.send("something".encode())

def check_connection():
server.listen()
while True:
client_ip, client_address = server.accept()
client_list.append(client_ip)
threading.Thread(target=check_client, args=(client_ip,), daemon=True).start()

check_connection()

So what happens is you call the check_connection function to check for incoming connections. After it receives one, it appends the connection inside the client_list variable. At the same time, it creates a thread to the current connection, check_client, which checks for any info being sent. If there's an info being sent by one of your clients, it checks if the "condition" string is inside your sent data. If so, it sends "something" string into all of your clients with exception to itself. Take note that when you send data, it must be in bytes.

For the client side:

import socket
import threading

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "serverip"
server_port = 8888
server.connect((server_ip, server_port))

def receive_info():
while True:
data = server.recv(1024).decode()

if "something" in data:
print("Someone already sent something")

threading.Thread(target=receive_info, daemon=True).start()

while True:
user_input = input("Type 'condition': ")
server.send(user_input.encode())

What this only does is, it sends your input into the server. If you typed "condition" on your input, it will send "something" on the other clients except you. So you need to setup 2 more clients in order to see the results.

Don't forget to set server_ip and server_port's values!

using socket io how to share a message to all users of Room except sender

socket.emit is used to emit an event globally.All user connected to socket will will listen and get data. for sharing a message in room use

socket.broadcast.to(roomid).emit()

your code will be like that

socket.broadcast.to(event).emit( 'message' , {message:message,
socketId:socket.id
});

Socket.io : sending to all clients in a namespace, excluding sender

If the socket is connected to a namespace then you forward the message using socket.broadcast.emit

const app = require("express")();
const http = require("http").createServer(app);
const PORT = 3000;
const io = require("socket.io")(http);

app.get("/", (req, res) => {
res.send("<h1>This is a socket io server</h1>");
});

http.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});

io.of("/").on("connection", function(socket) {
});

io.of("chat").on("connection", function(socket) {
socket.on("message", data => {
// socket is connected to namespace so just broadcast the emit to other clients in the namespace
socket.broadcast.emit("message", {
user: socket.id,
message: data.message
});
});
});

Socket.io emits message to all clients except the one that initially emitted the original message

as @guico33 from Discord have noticed I've used socket object rather than io when emitting to all clients.

io.on('connect', socket => {
socket.on('event', message => {
socket.in(room).emit(...)
})
})

It should look like so:

  io.in(socket.handshake.query.room).emit(message, data)

rather than:

  socket.in(socket.handshake.query.room).emit(message, data)

Instead of socket.in(room).emit(...), I should've used io.in(room).emit(...) to send the response to everyone including the emitter.



Related Topics



Leave a reply



Submit