How to spin up a websocket server paralelly along with express app?NodeJS WebSocket Handshake Silently Failing?How to get the full url in Express?How to access the GET parameters after “?” in Express?Which WebSocket library to use in Android app?express-ws how to periodically check a custom event and take action automaticallyArchitecture with http server, websocket and expressWebSocket connection closes when server sends message only on port 80Node.js WebSockets - send data to server with BasicAuthUsing an Express Server along with a Websocket Server; how can I trigger a Websocket send event after a POST to one of my routes?call require not possible in other file .js for nodeJs and express project
Can you cover a cube with copies of this shape?
Co-worker is now managing my team. Does this mean that I'm being demoted?
Is this set open or closed (or both?)
At what temperature should the earth be cooked to prevent human infection?
...and then she held the gun
Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?
How can the US president give an order to a civilian?
Why do you need to heat the pan before heating the olive oil?
High-end PC graphics circa 1990?
Can I drive in EU states and Switzerland with German proof of a surrendered U.S. license?
What kind of chart is this?
Background for black and white chart
Is there a term for someone whose preferred policies are a mix of Left and Right?
what is "dot" sign in the •NO?
Why are almost all the people in this orchestra recording wearing headphones with one ear on and one ear off?
Why is Skinner so awkward in Hot Fuzz?
Does knowing the surface area of all faces uniquely determine a tetrahedron?
Why can't we feel the Earth's revolution?
How do I run a script as sudo at boot time on Ubuntu 18.04 Server?
On George Box, Galit Shmueli and the scientific method?
What is this plant I saw for sale at a Romanian farmer's market?
How to prevent cables getting intertwined
Is it a bad idea to have a pen name with only an initial for a surname?
Leaving job close to major deadlines
How to spin up a websocket server paralelly along with express app?
NodeJS WebSocket Handshake Silently Failing?How to get the full url in Express?How to access the GET parameters after “?” in Express?Which WebSocket library to use in Android app?express-ws how to periodically check a custom event and take action automaticallyArchitecture with http server, websocket and expressWebSocket connection closes when server sends message only on port 80Node.js WebSockets - send data to server with BasicAuthUsing an Express Server along with a Websocket Server; how can I trigger a Websocket send event after a POST to one of my routes?call require not possible in other file .js for nodeJs and express project
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an express application, and a websocket script.
I create the server and spinup the express app in a www.js file.
The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.
Here is the www.js
....
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
....
Here is the websocket.js
const WebSocket = require('ws');
const wss = new WebSocket.Server( port: 8080 );
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
I know the line const wss = new Websocket.Server( port: 8080 );
creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.
node.js express ws
add a comment |
I have an express application, and a websocket script.
I create the server and spinup the express app in a www.js file.
The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.
Here is the www.js
....
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
....
Here is the websocket.js
const WebSocket = require('ws');
const wss = new WebSocket.Server( port: 8080 );
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
I know the line const wss = new Websocket.Server( port: 8080 );
creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.
node.js express ws
add a comment |
I have an express application, and a websocket script.
I create the server and spinup the express app in a www.js file.
The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.
Here is the www.js
....
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
....
Here is the websocket.js
const WebSocket = require('ws');
const wss = new WebSocket.Server( port: 8080 );
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
I know the line const wss = new Websocket.Server( port: 8080 );
creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.
node.js express ws
I have an express application, and a websocket script.
I create the server and spinup the express app in a www.js file.
The problem I'm facing is that, I'm not able to instantiate the websocket inside the www.js file.
Here is the www.js
....
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
....
Here is the websocket.js
const WebSocket = require('ws');
const wss = new WebSocket.Server( port: 8080 );
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
I know the line const wss = new Websocket.Server( port: 8080 );
creates the websocket instance...but I want to be able to create this instance in www.js file and all the logic for websocket in a seperate file.
node.js express ws
node.js express ws
edited Mar 25 at 5:11
Goutam B Seervi
asked Mar 25 at 3:59
Goutam B SeerviGoutam B Seervi
4917
4917
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Pass the http server instance to the websocket server,
const wss = new Websocket.Server( server: server );
If you want to separate it as a single modules to a file,
const WebSocket = require('ws');
const initWSServer = (server) =>
const wss = new WebSocket.Server( server: server);
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
module.exports =
initWSServer
Then,
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
const ws_server = require('./websocket.js');
ws_server.initWSServer(server);
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%2f55331093%2fhow-to-spin-up-a-websocket-server-paralelly-along-with-express-app%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
Pass the http server instance to the websocket server,
const wss = new Websocket.Server( server: server );
If you want to separate it as a single modules to a file,
const WebSocket = require('ws');
const initWSServer = (server) =>
const wss = new WebSocket.Server( server: server);
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
module.exports =
initWSServer
Then,
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
const ws_server = require('./websocket.js');
ws_server.initWSServer(server);
add a comment |
Pass the http server instance to the websocket server,
const wss = new Websocket.Server( server: server );
If you want to separate it as a single modules to a file,
const WebSocket = require('ws');
const initWSServer = (server) =>
const wss = new WebSocket.Server( server: server);
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
module.exports =
initWSServer
Then,
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
const ws_server = require('./websocket.js');
ws_server.initWSServer(server);
add a comment |
Pass the http server instance to the websocket server,
const wss = new Websocket.Server( server: server );
If you want to separate it as a single modules to a file,
const WebSocket = require('ws');
const initWSServer = (server) =>
const wss = new WebSocket.Server( server: server);
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
module.exports =
initWSServer
Then,
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
const ws_server = require('./websocket.js');
ws_server.initWSServer(server);
Pass the http server instance to the websocket server,
const wss = new Websocket.Server( server: server );
If you want to separate it as a single modules to a file,
const WebSocket = require('ws');
const initWSServer = (server) =>
const wss = new WebSocket.Server( server: server);
wss.on('connection', function connection(ws)
ws.on('message', function incoming(message)
console.log('received: %s', message);
);
ws.send('something');
);
module.exports =
initWSServer
Then,
const server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
const ws_server = require('./websocket.js');
ws_server.initWSServer(server);
edited Mar 25 at 5:29
answered Mar 25 at 5:24
JanithJanith
1,766716
1,766716
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%2f55331093%2fhow-to-spin-up-a-websocket-server-paralelly-along-with-express-app%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