Express-Ws closing connection closes unexpectedlyWhat is Node.js' Connect, Express and “middleware”?How to get the full url in Express?How to access the GET parameters after “?” in Express?Node js ECONNRESETSocket.IO removes all connections when browser is closedWhy do many websocket libraries implement their own application-level heartbeats?websocket ping with node jsexpress-ws route unable to connect on herokuSocket.io Java client automatically disconnects after a few secondsHow do you create SSL encrypted WebSockets from the express-ws middle ware using the 4.x project structure?
LED not blinking when using a transistor
What aircraft was used as Air Force One for the flight between Southampton and Shannon?
Is it possible to fly backward if you have really strong headwind?
How can I make 12 tone and atonal melodies sound interesting?
Ability To Change Root User Password (Vulnerability?)
What does the pair of vertical lines in empirical entropy formula mean?
Who won a Game of Bar Dice?
Why is Na5 not played in this line of the French Defense, Advance Variation?
Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?
Java Servlet & JSP simple login
Separate SPI data
Why is long-term living in Almost-Earth causing severe health problems?
Why Does Mama Coco Look Old After Going to the Other World?
Confused with atmospheric pressure equals plastic balloon’s inner pressure
Should I refuse to be named as co-author of a low quality paper?
Translating “About that...”
How can I remove material from this wood beam?
Difference between prepositions in "...killed during/in the war"
Who is "He that flies" in Lord of the Rings?
Does putting salt first make it easier for attacker to bruteforce the hash?
Why do radiation hardened IC packages often have long leads?
Is Lambda Calculus purely syntactic?
Reference to understand the notation of orbital charts
What would be the way to say "just saying" in German? (Not the literal translation)
Express-Ws closing connection closes unexpectedly
What is Node.js' Connect, Express and “middleware”?How to get the full url in Express?How to access the GET parameters after “?” in Express?Node js ECONNRESETSocket.IO removes all connections when browser is closedWhy do many websocket libraries implement their own application-level heartbeats?websocket ping with node jsexpress-ws route unable to connect on herokuSocket.io Java client automatically disconnects after a few secondsHow do you create SSL encrypted WebSockets from the express-ws middle ware using the 4.x project structure?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to use express-ws to enable websocket for express. I have two express servers and both uses ws. The code for setting up the ws is exactly the same for both servers (except the path names). One of the server is working as expected. The other one, closes as soon as a connection is established with exit code 1005. As far as I have researched, I have found 1005 to be described as a special error code to be returned in cases of unexpected disconnection. I have tried logging inside the ws route handler and, it prints nothing. On 'connection' handler is also not fired.
const expressWs = require('express-ws')(server, null wsOptions: clientTracking: true );
//<... HTTP route setup and middleware mounting >
server.ws('/',(sck, req)=> {
sck.on('connection', (s1) =>
console.dir(s1);
console.log('Connected')
);
let interval = setInterval(() =>
expressWs.getWss().clients.forEach(s =>
if(s.readyState === WS_CONSTANTS.Open)
s.ping(null, false, null);
);
, WS_HEARTBEAT_DELAY);
sck.on('close', (ev) =>
console.dir(ev);
clearInterval(interval);
);
//<... Other code for route handling >
As you can see, I implemented a 'heartbeat' technique for sending a ping to client to keep connection alive. As stated, it worked perfectly for only one of the server. I am very confused as to why 1005 is returned.
EDIT 1
Following this post here, I tried updating ws package for express-ws manually. This still did not solve the issue and the connection is still dropped with 1005 as reported earlier.
node.js express ws
add a comment |
I am trying to use express-ws to enable websocket for express. I have two express servers and both uses ws. The code for setting up the ws is exactly the same for both servers (except the path names). One of the server is working as expected. The other one, closes as soon as a connection is established with exit code 1005. As far as I have researched, I have found 1005 to be described as a special error code to be returned in cases of unexpected disconnection. I have tried logging inside the ws route handler and, it prints nothing. On 'connection' handler is also not fired.
const expressWs = require('express-ws')(server, null wsOptions: clientTracking: true );
//<... HTTP route setup and middleware mounting >
server.ws('/',(sck, req)=> {
sck.on('connection', (s1) =>
console.dir(s1);
console.log('Connected')
);
let interval = setInterval(() =>
expressWs.getWss().clients.forEach(s =>
if(s.readyState === WS_CONSTANTS.Open)
s.ping(null, false, null);
);
, WS_HEARTBEAT_DELAY);
sck.on('close', (ev) =>
console.dir(ev);
clearInterval(interval);
);
//<... Other code for route handling >
As you can see, I implemented a 'heartbeat' technique for sending a ping to client to keep connection alive. As stated, it worked perfectly for only one of the server. I am very confused as to why 1005 is returned.
EDIT 1
Following this post here, I tried updating ws package for express-ws manually. This still did not solve the issue and the connection is still dropped with 1005 as reported earlier.
node.js express ws
add a comment |
I am trying to use express-ws to enable websocket for express. I have two express servers and both uses ws. The code for setting up the ws is exactly the same for both servers (except the path names). One of the server is working as expected. The other one, closes as soon as a connection is established with exit code 1005. As far as I have researched, I have found 1005 to be described as a special error code to be returned in cases of unexpected disconnection. I have tried logging inside the ws route handler and, it prints nothing. On 'connection' handler is also not fired.
const expressWs = require('express-ws')(server, null wsOptions: clientTracking: true );
//<... HTTP route setup and middleware mounting >
server.ws('/',(sck, req)=> {
sck.on('connection', (s1) =>
console.dir(s1);
console.log('Connected')
);
let interval = setInterval(() =>
expressWs.getWss().clients.forEach(s =>
if(s.readyState === WS_CONSTANTS.Open)
s.ping(null, false, null);
);
, WS_HEARTBEAT_DELAY);
sck.on('close', (ev) =>
console.dir(ev);
clearInterval(interval);
);
//<... Other code for route handling >
As you can see, I implemented a 'heartbeat' technique for sending a ping to client to keep connection alive. As stated, it worked perfectly for only one of the server. I am very confused as to why 1005 is returned.
EDIT 1
Following this post here, I tried updating ws package for express-ws manually. This still did not solve the issue and the connection is still dropped with 1005 as reported earlier.
node.js express ws
I am trying to use express-ws to enable websocket for express. I have two express servers and both uses ws. The code for setting up the ws is exactly the same for both servers (except the path names). One of the server is working as expected. The other one, closes as soon as a connection is established with exit code 1005. As far as I have researched, I have found 1005 to be described as a special error code to be returned in cases of unexpected disconnection. I have tried logging inside the ws route handler and, it prints nothing. On 'connection' handler is also not fired.
const expressWs = require('express-ws')(server, null wsOptions: clientTracking: true );
//<... HTTP route setup and middleware mounting >
server.ws('/',(sck, req)=> {
sck.on('connection', (s1) =>
console.dir(s1);
console.log('Connected')
);
let interval = setInterval(() =>
expressWs.getWss().clients.forEach(s =>
if(s.readyState === WS_CONSTANTS.Open)
s.ping(null, false, null);
);
, WS_HEARTBEAT_DELAY);
sck.on('close', (ev) =>
console.dir(ev);
clearInterval(interval);
);
//<... Other code for route handling >
As you can see, I implemented a 'heartbeat' technique for sending a ping to client to keep connection alive. As stated, it worked perfectly for only one of the server. I am very confused as to why 1005 is returned.
EDIT 1
Following this post here, I tried updating ws package for express-ws manually. This still did not solve the issue and the connection is still dropped with 1005 as reported earlier.
node.js express ws
node.js express ws
edited Mar 25 at 11:15
Abrar Hossain
asked Mar 24 at 20:53
Abrar HossainAbrar Hossain
4133821
4133821
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Okay I have finally found the error. In my code for the server, I had the following:
server.get('*', async (req, res, nxt) =>
nxt(new Error('Unsupported Method'));
);
I had this to prevent any get requests from being processed since, the API solely relies on post routes. I changed it to the following:
server.get('*', async (req, res, nxt) =>
nxt();
);
This solved the problem. IMHO, I think the ws 'upgrade' happens over GET and responding with an error to get requests was blocking any upgrade request. Also, I ensured that the other server had no such blocking and this is why I initially assumed that both the servers had the same setup and this was confusing me. Hope this was helpful.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328461%2fexpress-ws-closing-connection-closes-unexpectedly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Okay I have finally found the error. In my code for the server, I had the following:
server.get('*', async (req, res, nxt) =>
nxt(new Error('Unsupported Method'));
);
I had this to prevent any get requests from being processed since, the API solely relies on post routes. I changed it to the following:
server.get('*', async (req, res, nxt) =>
nxt();
);
This solved the problem. IMHO, I think the ws 'upgrade' happens over GET and responding with an error to get requests was blocking any upgrade request. Also, I ensured that the other server had no such blocking and this is why I initially assumed that both the servers had the same setup and this was confusing me. Hope this was helpful.
add a comment |
Okay I have finally found the error. In my code for the server, I had the following:
server.get('*', async (req, res, nxt) =>
nxt(new Error('Unsupported Method'));
);
I had this to prevent any get requests from being processed since, the API solely relies on post routes. I changed it to the following:
server.get('*', async (req, res, nxt) =>
nxt();
);
This solved the problem. IMHO, I think the ws 'upgrade' happens over GET and responding with an error to get requests was blocking any upgrade request. Also, I ensured that the other server had no such blocking and this is why I initially assumed that both the servers had the same setup and this was confusing me. Hope this was helpful.
add a comment |
Okay I have finally found the error. In my code for the server, I had the following:
server.get('*', async (req, res, nxt) =>
nxt(new Error('Unsupported Method'));
);
I had this to prevent any get requests from being processed since, the API solely relies on post routes. I changed it to the following:
server.get('*', async (req, res, nxt) =>
nxt();
);
This solved the problem. IMHO, I think the ws 'upgrade' happens over GET and responding with an error to get requests was blocking any upgrade request. Also, I ensured that the other server had no such blocking and this is why I initially assumed that both the servers had the same setup and this was confusing me. Hope this was helpful.
Okay I have finally found the error. In my code for the server, I had the following:
server.get('*', async (req, res, nxt) =>
nxt(new Error('Unsupported Method'));
);
I had this to prevent any get requests from being processed since, the API solely relies on post routes. I changed it to the following:
server.get('*', async (req, res, nxt) =>
nxt();
);
This solved the problem. IMHO, I think the ws 'upgrade' happens over GET and responding with an error to get requests was blocking any upgrade request. Also, I ensured that the other server had no such blocking and this is why I initially assumed that both the servers had the same setup and this was confusing me. Hope this was helpful.
edited Mar 25 at 13:22
answered Mar 25 at 11:45
Abrar HossainAbrar Hossain
4133821
4133821
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328461%2fexpress-ws-closing-connection-closes-unexpectedly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown