Node.js HTTP server not able to respond to websocket Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!HTTP GET with request bodyHow do I debug Node.js applications?Writing files in Node.jsHow to use java.net.URLConnection to fire and handle HTTP requestsHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsUsing node.js as a simple web serverHow is an HTTP POST request made in node.js?What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

Generate an RGB colour grid

Do I really need to have a message in a novel to appeal to readers?

Is the Standard Deduction better than Itemized when both are the same amount?

Do wooden building fires get hotter than 600°C?

Do square wave exist?

What would be the ideal power source for a cybernetic eye?

Most bit efficient text communication method?

Around usage results

First console to have temporary backward compatibility

Can an alien society believe that their star system is the universe?

For a new assistant professor in CS, how to build/manage a publication pipeline

How do I make this wiring inside cabinet safer? (Pic)

Should I use a zero-interest credit card for a large one-time purchase?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

An adverb for when you're not exaggerating

How does the math work when buying airline miles?

How to answer "Have you ever been terminated?"

What's the meaning of "fortified infraction restraint"?

What does this Jacques Hadamard quote mean?

What font is "z" in "z-score"?

Is there a kind of relay only consumes power when switching?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?



Node.js HTTP server not able to respond to websocket



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!HTTP GET with request bodyHow do I debug Node.js applications?Writing files in Node.jsHow to use java.net.URLConnection to fire and handle HTTP requestsHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsUsing node.js as a simple web serverHow is an HTTP POST request made in node.js?What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















http server not responding to websocket



I am unable to have my http server respond to websocket requests. How do I correctly listen to these events? my server is running a SPA and I would like to try and use websockets to constantly send updated information from a file to the client. I have successfully done this with SSE but am curious about websockets.



My server.js has the following



const http = require('http');
const WebSocket = require('ws');

var server = http.createServer(function(request, response)
request.on('error', (err) =>
console.error(err.stack);
);
);

server.listen(8001);

const wss = new WebSocket.Server( server );

wss.on('connection', function connection(ws)
ws.on('message', function message(msg)
console.log(msg);
);
);
});


my client.js



var wsClient = ()=>
const ws = new WebSocket('ws:'+serverip+'/WSindexJSON'); // this server is set through XHR request

ws.onopen = function(e)
console.log("Connection established");
;

ws.addEventListener('open', () =>
// Send a message to the WebSocket server
ws.send('Hello!');
);
console.log("ws open",ws)

ws.addEventListener('message', event =>
// The `event` object is a typical DOM event object, and the message data sent
// by the server is stored in the `data` property
console.log('Received:', event.data);
);



I am able to see the client websocket created in the chrome developer network tools, but nothing is coming through on the server.
Do I need to create the websocket on the server side?



I have seen another example which the server listens for an upgrade event . I have tried that with no success.



server.on('upgrade', function upgrade(request, socket, head) 
const pathname = url.parse(request.url).pathname;
console.log('server upgrade event websocket', pathname)
if (pathname === '/WSindexJSON')
wss.handleUpgrade(request, socket, head, function done(ws)
wss.emit('connection', ws, request);
);
else
socket.destroy();

);


I have also tried another websocket module const WebSocketServer = require('websocket').server; and run into the same questions.










share|improve this question
























  • Your server code is not correctly closed!. You are missing closing }); for httpServer- after fixing that your code should work!

    – Milan
    Mar 8 at 18:53












  • Thanks for catching that I will edit my example here. This is not my full code just a summary of the http server. my server does work and I can connect to it respond with GET POST etc

    – colin rosati
    Mar 8 at 18:59

















0















http server not responding to websocket



I am unable to have my http server respond to websocket requests. How do I correctly listen to these events? my server is running a SPA and I would like to try and use websockets to constantly send updated information from a file to the client. I have successfully done this with SSE but am curious about websockets.



My server.js has the following



const http = require('http');
const WebSocket = require('ws');

var server = http.createServer(function(request, response)
request.on('error', (err) =>
console.error(err.stack);
);
);

server.listen(8001);

const wss = new WebSocket.Server( server );

wss.on('connection', function connection(ws)
ws.on('message', function message(msg)
console.log(msg);
);
);
});


my client.js



var wsClient = ()=>
const ws = new WebSocket('ws:'+serverip+'/WSindexJSON'); // this server is set through XHR request

ws.onopen = function(e)
console.log("Connection established");
;

ws.addEventListener('open', () =>
// Send a message to the WebSocket server
ws.send('Hello!');
);
console.log("ws open",ws)

ws.addEventListener('message', event =>
// The `event` object is a typical DOM event object, and the message data sent
// by the server is stored in the `data` property
console.log('Received:', event.data);
);



I am able to see the client websocket created in the chrome developer network tools, but nothing is coming through on the server.
Do I need to create the websocket on the server side?



I have seen another example which the server listens for an upgrade event . I have tried that with no success.



server.on('upgrade', function upgrade(request, socket, head) 
const pathname = url.parse(request.url).pathname;
console.log('server upgrade event websocket', pathname)
if (pathname === '/WSindexJSON')
wss.handleUpgrade(request, socket, head, function done(ws)
wss.emit('connection', ws, request);
);
else
socket.destroy();

);


I have also tried another websocket module const WebSocketServer = require('websocket').server; and run into the same questions.










share|improve this question
























  • Your server code is not correctly closed!. You are missing closing }); for httpServer- after fixing that your code should work!

    – Milan
    Mar 8 at 18:53












  • Thanks for catching that I will edit my example here. This is not my full code just a summary of the http server. my server does work and I can connect to it respond with GET POST etc

    – colin rosati
    Mar 8 at 18:59













0












0








0








http server not responding to websocket



I am unable to have my http server respond to websocket requests. How do I correctly listen to these events? my server is running a SPA and I would like to try and use websockets to constantly send updated information from a file to the client. I have successfully done this with SSE but am curious about websockets.



My server.js has the following



const http = require('http');
const WebSocket = require('ws');

var server = http.createServer(function(request, response)
request.on('error', (err) =>
console.error(err.stack);
);
);

server.listen(8001);

const wss = new WebSocket.Server( server );

wss.on('connection', function connection(ws)
ws.on('message', function message(msg)
console.log(msg);
);
);
});


my client.js



var wsClient = ()=>
const ws = new WebSocket('ws:'+serverip+'/WSindexJSON'); // this server is set through XHR request

ws.onopen = function(e)
console.log("Connection established");
;

ws.addEventListener('open', () =>
// Send a message to the WebSocket server
ws.send('Hello!');
);
console.log("ws open",ws)

ws.addEventListener('message', event =>
// The `event` object is a typical DOM event object, and the message data sent
// by the server is stored in the `data` property
console.log('Received:', event.data);
);



I am able to see the client websocket created in the chrome developer network tools, but nothing is coming through on the server.
Do I need to create the websocket on the server side?



I have seen another example which the server listens for an upgrade event . I have tried that with no success.



server.on('upgrade', function upgrade(request, socket, head) 
const pathname = url.parse(request.url).pathname;
console.log('server upgrade event websocket', pathname)
if (pathname === '/WSindexJSON')
wss.handleUpgrade(request, socket, head, function done(ws)
wss.emit('connection', ws, request);
);
else
socket.destroy();

);


I have also tried another websocket module const WebSocketServer = require('websocket').server; and run into the same questions.










share|improve this question
















http server not responding to websocket



I am unable to have my http server respond to websocket requests. How do I correctly listen to these events? my server is running a SPA and I would like to try and use websockets to constantly send updated information from a file to the client. I have successfully done this with SSE but am curious about websockets.



My server.js has the following



const http = require('http');
const WebSocket = require('ws');

var server = http.createServer(function(request, response)
request.on('error', (err) =>
console.error(err.stack);
);
);

server.listen(8001);

const wss = new WebSocket.Server( server );

wss.on('connection', function connection(ws)
ws.on('message', function message(msg)
console.log(msg);
);
);
});


my client.js



var wsClient = ()=>
const ws = new WebSocket('ws:'+serverip+'/WSindexJSON'); // this server is set through XHR request

ws.onopen = function(e)
console.log("Connection established");
;

ws.addEventListener('open', () =>
// Send a message to the WebSocket server
ws.send('Hello!');
);
console.log("ws open",ws)

ws.addEventListener('message', event =>
// The `event` object is a typical DOM event object, and the message data sent
// by the server is stored in the `data` property
console.log('Received:', event.data);
);



I am able to see the client websocket created in the chrome developer network tools, but nothing is coming through on the server.
Do I need to create the websocket on the server side?



I have seen another example which the server listens for an upgrade event . I have tried that with no success.



server.on('upgrade', function upgrade(request, socket, head) 
const pathname = url.parse(request.url).pathname;
console.log('server upgrade event websocket', pathname)
if (pathname === '/WSindexJSON')
wss.handleUpgrade(request, socket, head, function done(ws)
wss.emit('connection', ws, request);
);
else
socket.destroy();

);


I have also tried another websocket module const WebSocketServer = require('websocket').server; and run into the same questions.







node.js http websocket






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 19:01







colin rosati

















asked Mar 8 at 18:41









colin rosaticolin rosati

106




106












  • Your server code is not correctly closed!. You are missing closing }); for httpServer- after fixing that your code should work!

    – Milan
    Mar 8 at 18:53












  • Thanks for catching that I will edit my example here. This is not my full code just a summary of the http server. my server does work and I can connect to it respond with GET POST etc

    – colin rosati
    Mar 8 at 18:59

















  • Your server code is not correctly closed!. You are missing closing }); for httpServer- after fixing that your code should work!

    – Milan
    Mar 8 at 18:53












  • Thanks for catching that I will edit my example here. This is not my full code just a summary of the http server. my server does work and I can connect to it respond with GET POST etc

    – colin rosati
    Mar 8 at 18:59
















Your server code is not correctly closed!. You are missing closing }); for httpServer- after fixing that your code should work!

– Milan
Mar 8 at 18:53






Your server code is not correctly closed!. You are missing closing }); for httpServer- after fixing that your code should work!

– Milan
Mar 8 at 18:53














Thanks for catching that I will edit my example here. This is not my full code just a summary of the http server. my server does work and I can connect to it respond with GET POST etc

– colin rosati
Mar 8 at 18:59





Thanks for catching that I will edit my example here. This is not my full code just a summary of the http server. my server does work and I can connect to it respond with GET POST etc

– colin rosati
Mar 8 at 18:59












1 Answer
1






active

oldest

votes


















1














You need to specify port on which client will attempt to connect (in your case 8001):



var wsClient = ()=>{
const ws = new WebSocket('ws:'+serverip+':8001/WSindexJSON');





share|improve this answer

























  • Thank you I was the missing port.

    – colin rosati
    Mar 8 at 19:05











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069170%2fnode-js-http-server-not-able-to-respond-to-websocket%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









1














You need to specify port on which client will attempt to connect (in your case 8001):



var wsClient = ()=>{
const ws = new WebSocket('ws:'+serverip+':8001/WSindexJSON');





share|improve this answer

























  • Thank you I was the missing port.

    – colin rosati
    Mar 8 at 19:05















1














You need to specify port on which client will attempt to connect (in your case 8001):



var wsClient = ()=>{
const ws = new WebSocket('ws:'+serverip+':8001/WSindexJSON');





share|improve this answer

























  • Thank you I was the missing port.

    – colin rosati
    Mar 8 at 19:05













1












1








1







You need to specify port on which client will attempt to connect (in your case 8001):



var wsClient = ()=>{
const ws = new WebSocket('ws:'+serverip+':8001/WSindexJSON');





share|improve this answer















You need to specify port on which client will attempt to connect (in your case 8001):



var wsClient = ()=>{
const ws = new WebSocket('ws:'+serverip+':8001/WSindexJSON');






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 19:05

























answered Mar 8 at 19:00









MilanMilan

914714




914714












  • Thank you I was the missing port.

    – colin rosati
    Mar 8 at 19:05

















  • Thank you I was the missing port.

    – colin rosati
    Mar 8 at 19:05
















Thank you I was the missing port.

– colin rosati
Mar 8 at 19:05





Thank you I was the missing port.

– colin rosati
Mar 8 at 19:05



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55069170%2fnode-js-http-server-not-able-to-respond-to-websocket%23new-answer', 'question_page');

);

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







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко