Unable to connect to a websocket in node js
I have a websocket with URL "ws://localhost:1122" running in my machine. I am able to connect to the websocket when using it with normal javascript and deploying it in a jetty server.
But while using node js I could not connect to the web socket. I use the following code in both cases
var socket = new WebSocket(URL);
While using node js I have addded the following var WebSocket = require("ws")
. But the readyState of the socket never become OPEN in node js.
Is there anything I am missing in node js because while using jetty as a server and normal javascript I am able to connect to the websocket.
Here is my app.js
var http = require('http');
http.createServer(function (req, res)
res.writeHead(200, 'Content-Type': 'text/plain' );
res.end('Hello Worldn');
).listen(1131, "localhost");
var WebSocket = require("ws");
var wsUri = "ws://localhost:1122";
var socket;
printer();
function printer()
socket = new WebSocket(wsUri);
console.log(socket.readyState);
javascript node.js websocket
add a comment |
I have a websocket with URL "ws://localhost:1122" running in my machine. I am able to connect to the websocket when using it with normal javascript and deploying it in a jetty server.
But while using node js I could not connect to the web socket. I use the following code in both cases
var socket = new WebSocket(URL);
While using node js I have addded the following var WebSocket = require("ws")
. But the readyState of the socket never become OPEN in node js.
Is there anything I am missing in node js because while using jetty as a server and normal javascript I am able to connect to the websocket.
Here is my app.js
var http = require('http');
http.createServer(function (req, res)
res.writeHead(200, 'Content-Type': 'text/plain' );
res.end('Hello Worldn');
).listen(1131, "localhost");
var WebSocket = require("ws");
var wsUri = "ws://localhost:1122";
var socket;
printer();
function printer()
socket = new WebSocket(wsUri);
console.log(socket.readyState);
javascript node.js websocket
Can you post the whole server code ? You may have an error when or before you try to make the server listen.
– Seblor
Nov 14 '18 at 13:20
Updated the question with app.js.
– venkat
Nov 14 '18 at 13:48
The server is listening to the port1131
. You cannot connect with the port1122
on the client.
– Seblor
Nov 14 '18 at 14:08
add a comment |
I have a websocket with URL "ws://localhost:1122" running in my machine. I am able to connect to the websocket when using it with normal javascript and deploying it in a jetty server.
But while using node js I could not connect to the web socket. I use the following code in both cases
var socket = new WebSocket(URL);
While using node js I have addded the following var WebSocket = require("ws")
. But the readyState of the socket never become OPEN in node js.
Is there anything I am missing in node js because while using jetty as a server and normal javascript I am able to connect to the websocket.
Here is my app.js
var http = require('http');
http.createServer(function (req, res)
res.writeHead(200, 'Content-Type': 'text/plain' );
res.end('Hello Worldn');
).listen(1131, "localhost");
var WebSocket = require("ws");
var wsUri = "ws://localhost:1122";
var socket;
printer();
function printer()
socket = new WebSocket(wsUri);
console.log(socket.readyState);
javascript node.js websocket
I have a websocket with URL "ws://localhost:1122" running in my machine. I am able to connect to the websocket when using it with normal javascript and deploying it in a jetty server.
But while using node js I could not connect to the web socket. I use the following code in both cases
var socket = new WebSocket(URL);
While using node js I have addded the following var WebSocket = require("ws")
. But the readyState of the socket never become OPEN in node js.
Is there anything I am missing in node js because while using jetty as a server and normal javascript I am able to connect to the websocket.
Here is my app.js
var http = require('http');
http.createServer(function (req, res)
res.writeHead(200, 'Content-Type': 'text/plain' );
res.end('Hello Worldn');
).listen(1131, "localhost");
var WebSocket = require("ws");
var wsUri = "ws://localhost:1122";
var socket;
printer();
function printer()
socket = new WebSocket(wsUri);
console.log(socket.readyState);
javascript node.js websocket
javascript node.js websocket
edited Nov 14 '18 at 13:48
venkat
asked Nov 14 '18 at 13:11
venkatvenkat
17819
17819
Can you post the whole server code ? You may have an error when or before you try to make the server listen.
– Seblor
Nov 14 '18 at 13:20
Updated the question with app.js.
– venkat
Nov 14 '18 at 13:48
The server is listening to the port1131
. You cannot connect with the port1122
on the client.
– Seblor
Nov 14 '18 at 14:08
add a comment |
Can you post the whole server code ? You may have an error when or before you try to make the server listen.
– Seblor
Nov 14 '18 at 13:20
Updated the question with app.js.
– venkat
Nov 14 '18 at 13:48
The server is listening to the port1131
. You cannot connect with the port1122
on the client.
– Seblor
Nov 14 '18 at 14:08
Can you post the whole server code ? You may have an error when or before you try to make the server listen.
– Seblor
Nov 14 '18 at 13:20
Can you post the whole server code ? You may have an error when or before you try to make the server listen.
– Seblor
Nov 14 '18 at 13:20
Updated the question with app.js.
– venkat
Nov 14 '18 at 13:48
Updated the question with app.js.
– venkat
Nov 14 '18 at 13:48
The server is listening to the port
1131
. You cannot connect with the port 1122
on the client.– Seblor
Nov 14 '18 at 14:08
The server is listening to the port
1131
. You cannot connect with the port 1122
on the client.– Seblor
Nov 14 '18 at 14:08
add a comment |
1 Answer
1
active
oldest
votes
Because of the asynchronous nature of Node.js, socket.readyState
doesn't immediately reflect the state of the connection.
Instead, you should use the event emitter interface to check if/when the connection is being made:
socket = new WebSocket(wsUri);
socket.on('open', function()
console.log('connection opened');
).on('close', function()
console.log('connection closed');
).on('error', function(e)
console.log('connection error', e);
);
robertklep - I used this, the socket.on('open') functions gets skipped due to the asynchronous nature of node js. But executes once the connection is made. Is there a way to make this synchronous?
– venkat
Nov 15 '18 at 4:51
"executes once the connection is made". That's how event emitters work: they call the function when a particular event occurs :D You can't make it synchronous.
– robertklep
Nov 15 '18 at 7:21
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%2f53301039%2funable-to-connect-to-a-websocket-in-node-js%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
Because of the asynchronous nature of Node.js, socket.readyState
doesn't immediately reflect the state of the connection.
Instead, you should use the event emitter interface to check if/when the connection is being made:
socket = new WebSocket(wsUri);
socket.on('open', function()
console.log('connection opened');
).on('close', function()
console.log('connection closed');
).on('error', function(e)
console.log('connection error', e);
);
robertklep - I used this, the socket.on('open') functions gets skipped due to the asynchronous nature of node js. But executes once the connection is made. Is there a way to make this synchronous?
– venkat
Nov 15 '18 at 4:51
"executes once the connection is made". That's how event emitters work: they call the function when a particular event occurs :D You can't make it synchronous.
– robertklep
Nov 15 '18 at 7:21
add a comment |
Because of the asynchronous nature of Node.js, socket.readyState
doesn't immediately reflect the state of the connection.
Instead, you should use the event emitter interface to check if/when the connection is being made:
socket = new WebSocket(wsUri);
socket.on('open', function()
console.log('connection opened');
).on('close', function()
console.log('connection closed');
).on('error', function(e)
console.log('connection error', e);
);
robertklep - I used this, the socket.on('open') functions gets skipped due to the asynchronous nature of node js. But executes once the connection is made. Is there a way to make this synchronous?
– venkat
Nov 15 '18 at 4:51
"executes once the connection is made". That's how event emitters work: they call the function when a particular event occurs :D You can't make it synchronous.
– robertklep
Nov 15 '18 at 7:21
add a comment |
Because of the asynchronous nature of Node.js, socket.readyState
doesn't immediately reflect the state of the connection.
Instead, you should use the event emitter interface to check if/when the connection is being made:
socket = new WebSocket(wsUri);
socket.on('open', function()
console.log('connection opened');
).on('close', function()
console.log('connection closed');
).on('error', function(e)
console.log('connection error', e);
);
Because of the asynchronous nature of Node.js, socket.readyState
doesn't immediately reflect the state of the connection.
Instead, you should use the event emitter interface to check if/when the connection is being made:
socket = new WebSocket(wsUri);
socket.on('open', function()
console.log('connection opened');
).on('close', function()
console.log('connection closed');
).on('error', function(e)
console.log('connection error', e);
);
answered Nov 14 '18 at 14:33
robertkleprobertklep
137k18234244
137k18234244
robertklep - I used this, the socket.on('open') functions gets skipped due to the asynchronous nature of node js. But executes once the connection is made. Is there a way to make this synchronous?
– venkat
Nov 15 '18 at 4:51
"executes once the connection is made". That's how event emitters work: they call the function when a particular event occurs :D You can't make it synchronous.
– robertklep
Nov 15 '18 at 7:21
add a comment |
robertklep - I used this, the socket.on('open') functions gets skipped due to the asynchronous nature of node js. But executes once the connection is made. Is there a way to make this synchronous?
– venkat
Nov 15 '18 at 4:51
"executes once the connection is made". That's how event emitters work: they call the function when a particular event occurs :D You can't make it synchronous.
– robertklep
Nov 15 '18 at 7:21
robertklep - I used this, the socket.on('open') functions gets skipped due to the asynchronous nature of node js. But executes once the connection is made. Is there a way to make this synchronous?
– venkat
Nov 15 '18 at 4:51
robertklep - I used this, the socket.on('open') functions gets skipped due to the asynchronous nature of node js. But executes once the connection is made. Is there a way to make this synchronous?
– venkat
Nov 15 '18 at 4:51
"executes once the connection is made". That's how event emitters work: they call the function when a particular event occurs :D You can't make it synchronous.
– robertklep
Nov 15 '18 at 7:21
"executes once the connection is made". That's how event emitters work: they call the function when a particular event occurs :D You can't make it synchronous.
– robertklep
Nov 15 '18 at 7:21
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%2f53301039%2funable-to-connect-to-a-websocket-in-node-js%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
Can you post the whole server code ? You may have an error when or before you try to make the server listen.
– Seblor
Nov 14 '18 at 13:20
Updated the question with app.js.
– venkat
Nov 14 '18 at 13:48
The server is listening to the port
1131
. You cannot connect with the port1122
on the client.– Seblor
Nov 14 '18 at 14:08