Retrieve a specific websocket connection - go

I am trying to send a message to a WebSocket by referencing it's specific connection, which I maintain in a map of map[*websocket.Conn]bool. I do not want to broadcast the message using a channel but instead want to send a direct message to a specific connection.
(godebug) p clients.conn
map[*websocket.Conn]bool{(*websocket.Conn)(0xc420126000):true, (*websocket.Conn)(0xc4200f86c0):true}
I have opted out of Gorilla for now and am using the x/net/websocket sub-repository.
Any tips would be appreciated.

Related

How to store websocket id in Gorilla using Redis for accessing the socket connection across the processes

I would like to know the best way to store the websocket client id or connection in Redis. I am not building a chat app so I do not need to broadcast but send messages to specific websockets.
Need help.

Connect/disconnect from ActiveMQ topic on camel websocket connection/disconnection

I've got the following camel route which listens for messages on an ActiveMQ topic and immediately sends them to all connected web socket clients. This is working fine, but the connection to the topic is made as soon as the route builder is initialised.
from("activemq:topic:mytopic").routeId("routeid").to("websocket://test?sendToAll=true");
What I need is to only connect to the topic when one or more clients are connected to the web socket. Once there are no more connections I want to stop listening on the topic. Is this possible?
According to me there is no proper way to do this. The only way this can be achieved is override Jetty WebSocket code. Once you override Jetty Websocket code you get the flexibility to write your own custom code in open and close websocket.
Maintain a List for all websocket clients in open websocket. Check for close websocket and remove it from the list to know how many are connected or disconnected. Or keep a counter on open and close websocket.
Once all websocket clients get closed suspend the route so that your messages stay in the topic or queue.
If any client gets connected to websocket, resume the route so that the messages reach the particular client connected.

How to get full duplex websocket with Bottle and MQTT

I'm trying to get a "communication line" between a server app that uses MQTT for messaging and a web page where I want to see the messages in real time and send back messages to the server-side app.
I use mosquitto, Bottle and gevent on the server and I want to keep it as simple as possible. Using gevent I managed to receive the MQTT messages in a greenlet, put them in a queue and send the messages to the webpage in the websocket procedure which looks like this:
while True:
mqt = queue.get(True)
ws.send(mqt)
I can also send messages from the web page back to the server and MQTT like this (also through a queue):
while True:
msg = ws.receive()
queue2.put(msg)
However I want these two loops to work at the same time on the same websocket. Is there any way to combine them? For example does receive have a timeout? I guess I could use two separate websockets, but that would be a waste if I can do it with only one.
Why not just have messages delivered directly to the page using MQTT over Websockets? There are a number of brokers that support Websockets and the paho JavaScript client allows both subscribing and publishing of messages

How to fetch 0mq socket structure from fd?

We have an application in which we are listening on a socket. When clients connect, we need to know the per client “fd” and the peer address. This info can be fetched using the socket monitors. Subsequently, we need to send data separately for each client. (not send same data to all clients). Is there a standard API to get socket structure from “fd” which we can use in send API?
My understanding is that fd is only used as a field of the zmq_pollitem_t struct used in zmq_poll or other pollers. There it is used for when you want to interface ZMQ sockets with other non-ZMQ pollers (so if you're using zmq_poll it shouldn't be set to anything other than 0). So, in the case you are using a non-ZMQ poller, you're setting the fd value, not getting it.
Is this the use of fd you're referring to? If not, could you update your question to be more specific?

em-websocket with private channels

I need to create a web-socket server, I am planning to use em-websocket. I need to support private channels, that is I should be able to send message to one particular channel or subscriber.
There are many good chat examples that are in broadcast mode ie. they send the response to all the connected clients but I need to track each connected client somehow, and respond to each one separately. In other way, I need one private channel for each client so that I can respond to each client separately instead of broadcasting to all connected ones, make each web-socket connection unique
Can anyone guide me to right direction for implementing this or may be some example or gist.
Regards.

Resources