how to listen for all active websockets messages in javascript - websocket

hey guys so i have this email provider called https://emailfake.com/channel6/ i want a script that will listen for all ws comming messages for already opened connections
const socket = new WebSocket('wss://emailfake.com');
// Listen for messages
socket.addEventListener('message', (event) => {
console.log('Message from server ', event.data);
});
note : a script that will work on console of devtools

Related

Is it possible for websocket.addeventlistener to work with chrome exetnsion manifest v3?

Sending a websocket message was possible but listening for messages was not possible
Code:
websocket.addEventListener('message', (event) => {
console.log("Time to move to")
player.currentTime = parseFloat(event.data)
})

Socket.io don't emit after disconnect event

I want to send data from the server to the client after a disconnect socket.io event occurs.
Server :
const clients = {};
io.on('connection', socket => {
clients[socket.id] = socket;
console.log('connect : ' + socket.id);
socket.emit('socket-connect', 'connected');
socket.on('disconnect', () => {
socket.emit('socket-disconnect', 'disconnected');
delete clients[socket.id];
console.log('disconnect : ' + socket.id);
});
});
Client :
const socket = openSocket('localhost');
socket.on('socket-connect', data => {
console.log(data);
});
socket.on('socket-disconnect', data => {
console.log(data);
});
If socket connected, the server send a connect message to client and it works, but it does not send interrupted messages to the client if the disconnection occurs.
console.log('disconnect : ' + socket.id); on server showing disconnect with the socket.id.
I'm using socket.io v2.0.4 both on server and client.
How can I send data from the server if a socket disconnect event occurs?
Event: disconnect is fired upon disconnection. This simply means, for some reason, there is no connection between the client and the server.
So in this situation, you cannot send data to the client as the client is not connected. Disconnection may happen if the client's network is not available(read Internet failure) or the client environment(ex: browser) crashed or the user closed the tab.

Socket.emit() outside socket.on()

Do we always have to use socket.emit() inside a socket.on() like that:
io.sockets.on('connection', function (socket) {
console.log('User connected !');
retrieveDictionnary((dictionnary) =>{
socket.emit('dictionnarySend', dictionnary);
}
}
I want to create on my client side a function which ask information to the server when I click on a button:
translateServer(parameter, control){
this.socket.emit('translate', [parameter,control]);
}
But it seems that it's not working, the server never receive this message.
Thank you !
The pattern you are using above is the recommended way of interacting with a socket (ie acquiring a socket instance when the 'connection' event fires, and then calling emit() from that socket instance, etc).
If I understand your client-side requirements correctly, you are wanting to send data to the server via web sockets - are you sure the socket that you have established a web socket connection between the client and server?
For instance, if you add the following to your client-side code, you should see a success message in your console:
const socket = io.connect('YOUR SERVER ADDRESS');
socket.on('connect', () => {
console.log('connected to server!');
// [UPDATE]
// This assumes you have a <button> element on your page. When
// clicked, a message will be sent to the server via sockets
document.querySelector('button').addEventListener('click', (event) => {
// Prevent button click reloading page
event.preventDefault();
// Send message to server via socket
socket.emit('MESSAGE_ID', 'test message from client' + new Date());
});
});
Update
This shows your original server code, expanded with the detail needed to receive and print data sent from client via sockets:
io.sockets.on('connection', function (socket) {
console.log('User connected !');
// Register a server handler for any messages from client on MESSAGE_ID channel
socket.on('MESSAGE_ID', (message) => {
// Print the message received from client in console
console.log('message from client', message);
})
retrieveDictionnary((dictionnary) =>{
socket.emit('dictionnarySend', dictionnary);
}
}

Socket.io - Stop listening event client-side

Im trying to stop listening to an event (Client-side) on socket.io, I'm using this to listen to the event:
function connectWebSocket(channel){
var socket = io('https://mywebsite.com:3000');
socket.on('connect', () =>{
console.log('connected');
socket.on('test', function(data) {
alert('event call');
});
});
}
It works ok, but how i stop listening to the event 'test'?
I tried:
socket.off('test');
also tried:
socket.removeAllListeners('test);
and a lot of more commands, but nothing seems to work, as i started listening client-side, how i stop listening client-side?

Is it possible to use socket.io server with pure html5 websockets?

I want to use sockets in my web app. I don't want to use socket.io library on client-side. It's OK for server-side though. Can I do this?
Now with socket.io on server and pure websocket on client I have destroying non-socket.io upgrade error. I've googled that it means that I have to use socket.io-client library on client-side. Is there any way to avoid that? I don't want client to be tight with this library and use pure html5 websocket instead.
If it's not possible what should I use for server to connect with pure html5 websockets?
If someone is curious here is my server code (coffeescript file)
# Require HTTP module (to start server) and Socket.IO
http = require 'http'
io = require 'socket.io'
# Start the server at port 8080
server = http.createServer (req, res) ->
# Send HTML headers and message
res.writeHead 200, { 'Content-Type': 'text/html' }
res.end "<h1>Hello from server!</h1>"
server.listen 8080
# Create a Socket.IO instance, passing it our server
socket = io.listen server
# Add a connect listener
socket.on 'connection', (client) ->
# Create periodical which ends a message to the client every 5 seconds
interval = setInterval ->
client.send "This is a message from the server! #{new Date().getTime()}"
, 5000
# Success! Now listen to messages to be received
client.on 'message', (event) ->
console.log 'Received message from client!', event
client.on 'disconnect', ->
clearInterval interval
console.log 'Server has disconnected'
And here is a client-side
<script>
// Create a socket instance
socket = new WebSocket('ws://myservername:8080');
// Open the socket
socket.onopen = function (event) {
console.log('Socket opened on client side', event);
// Listen for messages
socket.onmessage = function (event) {
console.log('Client received a message', event);
};
// Listen for socket closes
socket.onclose = function (event) {
console.log('Client notified socket has closed', event);
};
};
</script>
I've found this library, seems OK for my needs https://npmjs.org/package/ws

Resources