socket.io id is not conform and don't work - javascript

Whene I get id of socket (socket.id) he return "/privateroom#cL-qy2G6gVfwUY_ZAAAH" the socket is in "/privateroom" namespace. but whene I want to send a message to socket with id that doesn't work. in documentation the id is just "cL-qy2G6gVfwUY_ZAAAH" i don't understand why that is different.
my socket version is ^2.3.0

Related

socket io not emitting messages to rooms

I have been stuck with this problem for a while now . I want to emit to all the connected clients of a room when a new client joins the room. I have already seen the https://socket.io/docs/v3/emit-cheatsheet/index.html and tried all the emit methods for rooms as seen below in server code and none of them work except the one which emits to all the connected clients and not to a specific room.
Server code
socket.on("newUser", (data) => {
socket.join(data.sessionId);
//not a single one of these works to emit to a specific room
socket.broadcast.to(data.sessionId).emit("userJoin", data);
io.to(data.sessionId).emit("userJoin", data);
io.in(data.sessionId).emit("userJoin", data);
io.sockets.in(data.sessionId).emit("userJoin", data);
//only this works but send to all the clients
io.emit("userJoin", data);
});
client code
//simply adds a div with info to inform other users that a new user has joined
//works only on io.emit() which emits to all the connected clients of every room
socket.on("userJoin",(data)=>{
const users=document.getElementById('users');
const noUsers=document.getElementById('noUsers');
if(data.action==='join'){
const user = document.createElement('article');
user.classList.add('card');
user.classList.add('lobby-item');
user.innerHTML=`<header class='card__header'>
<h1 class='session__id'>
${data.user.name}
</h1>
</header>`;
users.appendChild(user);
}
});
I think the problem with all the room emit methods not working might be with the rooms being formed wrongly or are empty.But still none of the methods for rooms work here.
Don't process overlep the socket event name and Check that the data object was sent correctly
example
io.to(data.sessionId).emit("userRoomJoin", data);
io.emit("userJoin", data);
Split the socket event name.
If it still doesn't work.
console.log(data.sessionId);
Check that the data object was not undefinded.
when you use an object into your code try to check is it undefinded or no !
so try this command :
console.log(data);
In socket io if there is a redirect from the app or a refresh from the client there is a new socket.id created which means that the old socket was disconnected and a new one was created.
In my app i was first doing socket.emit() on the join in route and then redirecting the user to a different lobby route which meant that the socket which joined the room had been disconnected due to redirecting it to lobby and a new socket was created which wasn't in any room.
Therefore io.to(data.sessionId).emit() wasn't working as all the socket that joined the room were disconnected due to redirecting. io.emit() works as it emits to all the sockets that are currently connected

Socket.io kick specific user

I have an event from the client side that passes the socket id to be kicked to the server, socket.emit('kick', socketId);.
My intention is to find the socket using this socketId server side, and then call socketToBeKicked.disconnect(). How do I find the specific socket? I tried looking at other posts but most of the solutions do not work anymore. Or is there a better way that I can implement the kick function?
I figured out a solution. Instead of trying to find the socket obj on the server side, we can emit an event from the server to the client to that is going to get kicked and emit another event back to the server to get the client's socket obj.
client.js
socket.emit('kick', socketId); // sent by the host
socket.on('kick helper', () => { // sent by the user getting kicked
socket.emit('kick helper');
}
server.js
socket.on('kick', (socketId) => {
socket.to(socketId).emit('kick helper');
}
socket.on('kick helper', () => { // now this socket obj is the user getting kicked
socket.disconnect();
}
It still feels like quite a long way around to get the socket object of the user to be kicked. Please share if you have a better solution, I would love to know!

How to use the correct protocol for receiving active mq web socket stomp from java based program

I have a JavaScript based active mq listener, who is using stomp and web sockets.I was able to send test messages to active mq and receive them.
What I really want is it needs to be sent from a Java based code.
Is it Ok to have the JavaScript listening on web sockets/stomp and
the java code be using tcp ?
If it is OK, should all of the ports be the same?
I am having issues receiving data in JavaScript. However I am seeing the topic being enquued in the active mq.thanks
function subscribeEndpoint(endpoint){
var client = Stomp.client("ws://localhost:61614/stomp", "v11.stomp");
var headers = { id:'JUST.FCX', ack: 'client'};
client.connect("admin", "admin", function () {
client.subscribe(endpoint,
function (message) {
alert(message);
}, headers);
});
}
Java:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616/stomp");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic)
Destination destination = session.createTopic("vrwrThreat");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage message = session.createTextMessage(text);
producer.send(message);
// Clean up
session.close();
connection.close();
Clients can communicate with each other across protocols and transport mechanisms without issue. A STOMP client on WebSocket or any other port (TCP, SSL, etc) can send to a Topic and an AMQP, MQTT or OpenWire client can receive the message so long as the protocol supports the content of the message, same goes for the reverse case of AMQP, MQTT or OpenWire sending and STOMP receiving.
The case you've mentioned is pretty standard. The code you've posted appears to be using the OpenWire client to send to a Topic "vrwrThreat". The URI used in the connection factory is slightly off in that you added the '/stomp' which is meaningless. Since you are sending to a Topic you need to ensure that the client that is to receive the message is active at the time of transmission otherwise the message will be dropped. Also you need to ensure that both are operating on the same Topic which is unclear from your code snippets.

disconnect client from socket.io id

I can connect specific player to specific room like:
socket.join('sampleroom');
And documentation says that 'you can use leave method to leave room' like:
socket.leave('sampleroom');
But I just want any client to leave using io object instead of socket. I need something like:
io.sockets(specificSocketID).leave('sampleroom');
Is there any way to leave client from room using just socket id with io object?
You can save the incoming sockets into your own object.
var sockets = {};
io.on('connection',function(socket){
sockets[specificSocketID] = socket;
io.on('disconnect',function(){
delete sockets[specificSocketID];
});
});
//then you can easily call a specific socket with its id
sockets[specificSocketID].join('sampleRoom');
sockets[specificSocketID].leave('sampleRoom');
As #tresdin mentioned, we can store socket ids, however io object is also stored so actually we don't need it. We can use specific socket id like
io.sockets.sockets[specificSocketID].leave('sampleroom');
For version > 3.0
io.sockets.sockets.get(specificSocketID).leave("sampleroom");
For version < 3.0
io.sockets.sockets[specificSocketID].leave("sampleroom");

When exactly Signalr IUserIdProvider's method GetUserId called by the signalr framework

When does the GetUserId method get called by the SignalR framework when we have a custom IUserIdProvider as follows:
public class RealTimeNotificationsUserIdProvider : IUserIdProvider
{
public string GetUserId(IRequest request)
{
return request.QueryString["userId"];
}
}
How and when does the ConnectionId to UserId mapping happen and how does the following method work:
Clients.User("1").notify(notificationsJson);
Does the user have to be authenticated for this to work?
The user does not need to be authenticated for a custom IUserIdProvider to work.
However, if the user is authenticated, and you don't replace the default IUserIdProvider with your own in SignalR's DependencyResolver, the client's user id will be equal to its IPrincipal.Identity.Name.
There isn't a mapping between the client's connection id and user id exactly, but when a client connects using SignalR, it will internally establish subscriptions to receive messages sent to both its connection id and user id. IUserIdProvider.GetUserId is invoked when each SignalR connection is established, so SignalR can set up the subscription.

Categories