Socket client is getting disconnected either due to transport close or pingtimeout error. And it happens randomly. Sometime the socket client is stable for couple of hours and after that is start disconnecting randomly.Can anyone help me finding the issue.
Socket-Client version : 2.1.0
Socket Server version : 2.1.0,
Client Code
const socket = require('socket.io-client')
let url = 'http://localhost:5050'
let clientSocket = socket.connect(url, {
reconnection: true,
forceNew: true,
secure: true
})
clientSocket.on("connect", function (data) {
// console.log(clientSocket)
console.log("connection established");
});
clientSocket.on("event", function(data) {
console.log(data)
})
Server Code
const socketio = require('socket.io');
this.io = socketio.listen(this.server,
{
'pingInterval': PING_INTERVAL,
'pingTimeout': PING_TIMEOUT
});
this.io.on('connection', function (socket) {
// const consumer = new ConsumerGroup(options, topic);
// reading data from add event and sending back the same data
console.log('Connected', socket.id);
const token = socket.handshake.query.token;
socket.on('disconnect', function () {
console.log(socket.id + ' -> Disconnected');
});
consumer.on('ready', function (message) {
console.log('Ready');
});
consumer.on('message', function (message) {
// sending message on socket when we recieve the message from kafka\
socket.emit('alarm', message);
});
consumer.on('error', function (err) {
console.log('error', err);
});
});
Related
I am emitting messages from socket.io server running on port 8001
but my socket.io client not able to connect and receive these messages
my index.html (client):
<script src="https://cdn.socket.io/socket.io-4.0.0.js"></script>
<script>
//var socket = io();
//var socket = io.connect('http://localhost:8001');
var socket = io('http://localhost:8001', { transports : ['websocket'] });
socket.on('connect', function(){
console.log("connected");
socket.on("message", data => {
console.log(data);
});
});
</script>
My nodejs server code:
const app = require("express")();
const server = require("http").createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: '*',
}
});
io.on("connection", () => {
console.log("Connected!");
});
var redis = require('redis');
//var url = "redis://:#localhost:6379";
//var redis = require('redis-url').connect();
//var client = redis.createClient(url);
var client = redis.createClient();
//var client = redis.createClient();
client.on("error", function(error) {
console.error(error);
});
client.subscribe('notification');
client.on('message', function(channel, msg) {
console.log("Message received: "+msg);
io.sockets.emit(msg);
});
console.log('starting server on 8001...');
server.listen(8001);
My node js server console logs:
starting server on 8001...
Message received: from laravel
io.sockets.send(msg);
this worked for me. also make sure you are using the same version of socket.io on both client and server
I'm working on socket room and i try to disconnect from client side when i refresh the page, the problem is in console i receive message that socket id is disconnected, and another socket id generated but the old socket still active
here is my code in client side :
var roomSocket = io.connect("http://mysitehere.com:5001", {'forceNew': true});
var room = port;
roomSocket.on('connect', function () {
roomSocket.emit('starting', room);
console.log('emit connection to room');
});
roomSocket.on('connected', function () {
console.log('connected user');
});
roomSocket.on('message', function (data) {
console.log('Incoming message:' + data);
});
$(document).unload(function () {
roomSocket.disconnect();
});
my code in server js
socket.on('disconnect', function (data) {
console.log('------------------------------------');
console.log("DISCONNECTION : " + socket.id);
console.log('------------------------------------');
});
and the result :
I am using the following code to create/subscribe to a topic and handle the messages. Sometimes the connection gets lost and the error says:
Whoops! The connection was lost...
I would like to know if there is a way to reconnect it. Is it possible in the error callback or define the whole code in a method and call it recursively in error callback ?
$(document).ready(function () {
........
...............
try {
var socket = new SockJS("${createLink(uri: '/stomp')}");
var client = Stomp.over(socket);
client.connect({}, function () {
client.subscribe("/topic/${userInstance?.username}",
function (message) {
............
....................
});
});
} catch (error) {
console.log("ERROR: " + error.toString());
}
});
I managed to do it using failure callback and connect again. It will keep trying as long as it will fail.
This is what I am using in a Polymer element:
ready: function() {
this.connectWs();
},
connectWs: function() {
this.socket = new WebSocket(this.socketUrl);
this.stompClient = Stomp.over(this.socket);
this.stompClient.debug = null;
this.stompClient.connect({},
function(frame) {
// Connection OK
}.bind(this),
function(e) {
console.error(e, "Reconnecting WS", this.socketUrl);
window.setTimeout(function() {
this.connectWs();
}.bind(this), 2500);
}.bind(this)
);
},
I am confused on what the 'socket' parameter is that is passed with the function (In 'The enigma' section). Then the parameter gets used 'socket.on'. What is the difference between io.on and socket.on?
The following code is slightly adapted from the Socket.io chat application example.
Variables
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app)
var io = require('socket.io').listen(server);
The enigma
io.on('connection', function (socket) {
console.log('user connected');
socket.on('message', function(msg) {
console.log('message: ' + msg);
io.emit('message', msg);
})
});
Start server
server.listen(3000, function() {
console.log('server is running');
});
index.jade
body
script(src="/socket.io/socket.io.js")
form(method='post', action="/")
input(type='text', id='user', autocomplete='off')
input(type='submit', onClick="myFunc()")
strong messages:
p(id="messages")
script.
var socket = io();
socket.on('message', function(msg) {
console.log('client: ' + msg);
});
function myFunc() {
var text = document.getElementById('user');
socket.emit('message', text.value);
text.value = '';
};
In your code example, io is a Socket.IO server instance attached to an instance of http.Server listening for incoming events.
The socket argument of the connection event listener callback function is an object that represents an incoming socket connection from a client.
Both of them can listen for events with the on method.
It might help you visually understand how the two are separate if you re-imagine your code sample like this:
var connectionEvent = function(socket) {
console.log('user connected');
socket.on('message', function(msg) {
console.log('message: ' + msg);
io.emit('message', msg);
});
};
io.on('connection', connectionEvent);
My node app posts an object (consisting of data collected in a form on the client) to Salesforce via their API. On receiving a success or error message, I would like to send it to the client-side, then display it. Socket.io seemed like the tool for this in my simple node/express3 app, but beyond the simple demo I'm not able to get data to pass between my server and my client.
My relevant server side code:
var express = require('express');
var port = 5432;
var app = module.exports = express();
var server = require('http').createServer(app);
var nforce = require('nforce');
var org = nforce.createConnection({
clientId: 'MY_CLIENT_ID',
clientSecret: 'MY_CLIENT_SECRET',
redirectUri: 'http://localhost:5432/oauth/_callback'
});
var io = require('socket.io').listen(server);
// here I authenticate with Salesforce, this works fine
app.post('/salesforce', function(req, res){
var lead = nforce.createSObject('Lead');
// here I construct the lead object, which also works fine
org.insert(lead, oauth, function(err, res) {
if (err === null) {
console.log(res);
leadSuccessMessage(res);
}
else {
console.log(err);
var error = {
errorCode: err.errorCode,
statusCode: err.statusCode,
messageBody: err.messageBody
};
console.log(error);
leadErrorMessage(error);
}
});
}
function leadSuccessMessage(res) {
var resp = res;
console.log('called success message from server');
io.sockets.on('connection', function (socket) {
socket.emit('sfRes', resp);
socket.on('thanks', function (data) {
console.log(data);
});
});
}
function leadErrorMessage(error) {
var err = error;
console.log('called error message from server');
io.sockets.on('connection', function (socket) {
console.log("socket is: " + socket);
socket.emit('sfRes', err);
socket.on('thanks', function (data) {
console.log(data);
});
});
}
And my relevant client side scripts:
<script src="/socket.io/socket.io.js"></script>
<script>
current.page = document.URL;
console.log("current page is: " + current.page);
var socket = io.connect(current.page);
socket.on('sfRes', function (data) {
console.log("client received: " + data);
fst.showLeadStatus(data);
socket.emit('thanks', {message: "received server feedback"});
});
</script>
When I post the form containing valid data using a spicy little AJAX call:
postToSF: function(){
$('#submitLead').on('click', function(e){
e.preventDefault();
var formData = $('#lead_form').serialize();
$.ajax({
type: 'POST',
url: '/salesforce',
data: formData,
success: function(){
fst.log('success!');
},
error: function(xhr, ajaxOptions, thrownError){
console.error(xhr.status); // 0
console.error(thrownError);
}
});
});
}
All I get are tears, and these in the server-side console:
// the result of `console.log(res)`
{ id: '00Qa000001FZfhKEAT', success: true, errors: [] }
// and proof that `leadSuccessMessage()` got called
called success message from server
Instead of calling this function from a client-side object as it's supposed to:
showLeadStatus: function(response){
if (response.success) {
fst.log("showing lead status as: " + response);
$('#leadStatus').addClass('success').removeClass('error').fadeIn().delay(4000).fadeOut();
}
else {
fst.log("showing lead status as: " + response);
$('#leadStatus').text(response.messageBody).addClass('error').removeClass('success').fadeIn().delay('4000').fadeOut();
}
$('#startOver').click();
}
Which works fine if I call it in the console passing it the data the server is supposed to be socketing over:
// this works, gosh darn it
fst.showLeadStatus({ id: '00Qa000001FZfhKEAT', success: true, errors: [] });
The Salesforce post error case doesn't surface anything to the client either. And there are no errors in the client or server console to contend with.
I'm stumped. Please help!
I would do something like this -
var mysocket = null;
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
mysocket = socket;
socket.on('thanks', function (data) {
console.log(data);
});
});
app.post('/salesforce', function(req, res){
....
....
})
function leadSuccessMessage(res) {
var resp = res;
console.log('called success message from server');
if(mysocket)
mysocket.emit('sfRes', resp);
}
function leadErrorMessage(error) {
var err = error;
console.log('called error message from server');
if(mysocket)
mysocket.emit('sfRes', err);
}