Simple example on how to use Websockets between Client and Server
I am new to websockets and just trying to get a handle of how to listen to a message from a client browser from the server and vice-versa.
I'm using a Node.js/Express setup and just want to be able to firstly listen for any messages from the client.
I've been looking at this https://github.com/websockets/ws library and have tried the examples but am not able to get this working within my localhost environment.
I'm also not clear what I need to look out for, when I'm listening for a message.
What code do I use on the client, i.e. url + port and what code do I use on the server?
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost/path',
perMessageDeflate: false
);
javascript node.js websocket socket.io
add a comment |
I am new to websockets and just trying to get a handle of how to listen to a message from a client browser from the server and vice-versa.
I'm using a Node.js/Express setup and just want to be able to firstly listen for any messages from the client.
I've been looking at this https://github.com/websockets/ws library and have tried the examples but am not able to get this working within my localhost environment.
I'm also not clear what I need to look out for, when I'm listening for a message.
What code do I use on the client, i.e. url + port and what code do I use on the server?
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost/path',
perMessageDeflate: false
);
javascript node.js websocket socket.io
websocket servers are listening for messages from clients (and viceversa), not the backend itself. What you want is some kind of error handling framework (depending on which lib throws the error it might implement onerror callback handlers)
– Adelin
Nov 14 '18 at 7:22
The title asks for one thing and the body asks for a completely different thing
– Adelin
Nov 14 '18 at 7:22
@Adelin Thanks for pointing that out. Have edited my original post.
– tonyf
Nov 14 '18 at 11:48
add a comment |
I am new to websockets and just trying to get a handle of how to listen to a message from a client browser from the server and vice-versa.
I'm using a Node.js/Express setup and just want to be able to firstly listen for any messages from the client.
I've been looking at this https://github.com/websockets/ws library and have tried the examples but am not able to get this working within my localhost environment.
I'm also not clear what I need to look out for, when I'm listening for a message.
What code do I use on the client, i.e. url + port and what code do I use on the server?
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost/path',
perMessageDeflate: false
);
javascript node.js websocket socket.io
I am new to websockets and just trying to get a handle of how to listen to a message from a client browser from the server and vice-versa.
I'm using a Node.js/Express setup and just want to be able to firstly listen for any messages from the client.
I've been looking at this https://github.com/websockets/ws library and have tried the examples but am not able to get this working within my localhost environment.
I'm also not clear what I need to look out for, when I'm listening for a message.
What code do I use on the client, i.e. url + port and what code do I use on the server?
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost/path',
perMessageDeflate: false
);
javascript node.js websocket socket.io
javascript node.js websocket socket.io
edited Nov 17 '18 at 16:34
halfer
14.5k758111
14.5k758111
asked Nov 14 '18 at 7:18
tonyftonyf
12.5k36114189
12.5k36114189
websocket servers are listening for messages from clients (and viceversa), not the backend itself. What you want is some kind of error handling framework (depending on which lib throws the error it might implement onerror callback handlers)
– Adelin
Nov 14 '18 at 7:22
The title asks for one thing and the body asks for a completely different thing
– Adelin
Nov 14 '18 at 7:22
@Adelin Thanks for pointing that out. Have edited my original post.
– tonyf
Nov 14 '18 at 11:48
add a comment |
websocket servers are listening for messages from clients (and viceversa), not the backend itself. What you want is some kind of error handling framework (depending on which lib throws the error it might implement onerror callback handlers)
– Adelin
Nov 14 '18 at 7:22
The title asks for one thing and the body asks for a completely different thing
– Adelin
Nov 14 '18 at 7:22
@Adelin Thanks for pointing that out. Have edited my original post.
– tonyf
Nov 14 '18 at 11:48
websocket servers are listening for messages from clients (and viceversa), not the backend itself. What you want is some kind of error handling framework (depending on which lib throws the error it might implement onerror callback handlers)
– Adelin
Nov 14 '18 at 7:22
websocket servers are listening for messages from clients (and viceversa), not the backend itself. What you want is some kind of error handling framework (depending on which lib throws the error it might implement onerror callback handlers)
– Adelin
Nov 14 '18 at 7:22
The title asks for one thing and the body asks for a completely different thing
– Adelin
Nov 14 '18 at 7:22
The title asks for one thing and the body asks for a completely different thing
– Adelin
Nov 14 '18 at 7:22
@Adelin Thanks for pointing that out. Have edited my original post.
– tonyf
Nov 14 '18 at 11:48
@Adelin Thanks for pointing that out. Have edited my original post.
– tonyf
Nov 14 '18 at 11:48
add a comment |
2 Answers
2
active
oldest
votes
Using websockets directly might be troublesome, it's advised you use a framework to abstract this layer, so they can easily fallback to other methods when not supported in the client. For example, this is a direct implementation using Express js and Websockets directly. This example also allows you to use the same server for HTTP calls.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
//initialize a simple http server
const server = http.createServer(app);
//initialize the WebSocket server instance
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
//connection is up, let's add a simple simple event
ws.on('message', (message) =>
//log the received message and send it back to the client
console.log('received: %s', message);
ws.send(`Hello, you sent -> $message`);
);
//send immediatly a feedback to the incoming connection
ws.send('Hi there, I am a WebSocket server');
);
//start our server
server.listen(3000, () =>
console.log(`Server started on port $server.address().port :)`);
);
For the client, you can do something like this:
const ws = new WebSocket('ws://localhost:3000')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received`, message.data)
Like i have mentioned above, it is advised that you use a mature framework for websockets. Should your app be minimal and not need scaling, you can use any open source library, with socket.io being the most popular.
However, if you are talking about implementing this to be used at production level, you should know that the open source solutions do not allow for scalability, failover, message ordering etc. In that case, you’ll have to implement a realtime platform as a service tool. Ably’s Data Stream Network is one such solution that I’ve personally used for a lot of my projects.
Have tried your example but on the client side as part of your ws.onmessage console log, I am receiving the output: message received [object MessageEvent] - what is this and what should have actually returned from the server as I don't think I am getting the correct message? Furthermore, in your server code, I believe all you "ws.on/send" should actually be "WebSocket.on/send"
– tonyf
Nov 14 '18 at 14:24
This is because the message variable is an object and was displayed in a string. you would need to accessmessage.data
to get the message that was actually sent. Updated the code to show that
– ayodele samuel
Nov 15 '18 at 18:23
add a comment |
Just a note, socket.io
is a backend/frontend library that uses websocket
but also has a number of fallbacks if the client browser does not support websocket. The example below works with ws
backend.
Server
const WS = require('ws')
const PORT = process.env.PORT || 8080
const wss = new WS.Server(
port: PORT
, () => console.log(`ws server live on $PORT`))
const errHandle = (err) =>
if(err) throw err
wss.on('connection', (socket) =>
console.log('something connected')
socket.send('you are connected', errHandle)
socket.on('message', (data) =>
console.log(`socket sent $data`)
socket.send('message received', errHandle)
)
)
client (browser)
(() =>
const ws = new WebSocket('ws://localhost:8080')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received $message`)
)()
edit: oh, and ws
and http
are different protocols. you will need a different server to serve your http files
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%2f53294938%2fsimple-example-on-how-to-use-websockets-between-client-and-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using websockets directly might be troublesome, it's advised you use a framework to abstract this layer, so they can easily fallback to other methods when not supported in the client. For example, this is a direct implementation using Express js and Websockets directly. This example also allows you to use the same server for HTTP calls.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
//initialize a simple http server
const server = http.createServer(app);
//initialize the WebSocket server instance
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
//connection is up, let's add a simple simple event
ws.on('message', (message) =>
//log the received message and send it back to the client
console.log('received: %s', message);
ws.send(`Hello, you sent -> $message`);
);
//send immediatly a feedback to the incoming connection
ws.send('Hi there, I am a WebSocket server');
);
//start our server
server.listen(3000, () =>
console.log(`Server started on port $server.address().port :)`);
);
For the client, you can do something like this:
const ws = new WebSocket('ws://localhost:3000')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received`, message.data)
Like i have mentioned above, it is advised that you use a mature framework for websockets. Should your app be minimal and not need scaling, you can use any open source library, with socket.io being the most popular.
However, if you are talking about implementing this to be used at production level, you should know that the open source solutions do not allow for scalability, failover, message ordering etc. In that case, you’ll have to implement a realtime platform as a service tool. Ably’s Data Stream Network is one such solution that I’ve personally used for a lot of my projects.
Have tried your example but on the client side as part of your ws.onmessage console log, I am receiving the output: message received [object MessageEvent] - what is this and what should have actually returned from the server as I don't think I am getting the correct message? Furthermore, in your server code, I believe all you "ws.on/send" should actually be "WebSocket.on/send"
– tonyf
Nov 14 '18 at 14:24
This is because the message variable is an object and was displayed in a string. you would need to accessmessage.data
to get the message that was actually sent. Updated the code to show that
– ayodele samuel
Nov 15 '18 at 18:23
add a comment |
Using websockets directly might be troublesome, it's advised you use a framework to abstract this layer, so they can easily fallback to other methods when not supported in the client. For example, this is a direct implementation using Express js and Websockets directly. This example also allows you to use the same server for HTTP calls.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
//initialize a simple http server
const server = http.createServer(app);
//initialize the WebSocket server instance
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
//connection is up, let's add a simple simple event
ws.on('message', (message) =>
//log the received message and send it back to the client
console.log('received: %s', message);
ws.send(`Hello, you sent -> $message`);
);
//send immediatly a feedback to the incoming connection
ws.send('Hi there, I am a WebSocket server');
);
//start our server
server.listen(3000, () =>
console.log(`Server started on port $server.address().port :)`);
);
For the client, you can do something like this:
const ws = new WebSocket('ws://localhost:3000')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received`, message.data)
Like i have mentioned above, it is advised that you use a mature framework for websockets. Should your app be minimal and not need scaling, you can use any open source library, with socket.io being the most popular.
However, if you are talking about implementing this to be used at production level, you should know that the open source solutions do not allow for scalability, failover, message ordering etc. In that case, you’ll have to implement a realtime platform as a service tool. Ably’s Data Stream Network is one such solution that I’ve personally used for a lot of my projects.
Have tried your example but on the client side as part of your ws.onmessage console log, I am receiving the output: message received [object MessageEvent] - what is this and what should have actually returned from the server as I don't think I am getting the correct message? Furthermore, in your server code, I believe all you "ws.on/send" should actually be "WebSocket.on/send"
– tonyf
Nov 14 '18 at 14:24
This is because the message variable is an object and was displayed in a string. you would need to accessmessage.data
to get the message that was actually sent. Updated the code to show that
– ayodele samuel
Nov 15 '18 at 18:23
add a comment |
Using websockets directly might be troublesome, it's advised you use a framework to abstract this layer, so they can easily fallback to other methods when not supported in the client. For example, this is a direct implementation using Express js and Websockets directly. This example also allows you to use the same server for HTTP calls.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
//initialize a simple http server
const server = http.createServer(app);
//initialize the WebSocket server instance
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
//connection is up, let's add a simple simple event
ws.on('message', (message) =>
//log the received message and send it back to the client
console.log('received: %s', message);
ws.send(`Hello, you sent -> $message`);
);
//send immediatly a feedback to the incoming connection
ws.send('Hi there, I am a WebSocket server');
);
//start our server
server.listen(3000, () =>
console.log(`Server started on port $server.address().port :)`);
);
For the client, you can do something like this:
const ws = new WebSocket('ws://localhost:3000')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received`, message.data)
Like i have mentioned above, it is advised that you use a mature framework for websockets. Should your app be minimal and not need scaling, you can use any open source library, with socket.io being the most popular.
However, if you are talking about implementing this to be used at production level, you should know that the open source solutions do not allow for scalability, failover, message ordering etc. In that case, you’ll have to implement a realtime platform as a service tool. Ably’s Data Stream Network is one such solution that I’ve personally used for a lot of my projects.
Using websockets directly might be troublesome, it's advised you use a framework to abstract this layer, so they can easily fallback to other methods when not supported in the client. For example, this is a direct implementation using Express js and Websockets directly. This example also allows you to use the same server for HTTP calls.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
//initialize a simple http server
const server = http.createServer(app);
//initialize the WebSocket server instance
const wss = new WebSocket.Server( server );
wss.on('connection', (ws) =>
//connection is up, let's add a simple simple event
ws.on('message', (message) =>
//log the received message and send it back to the client
console.log('received: %s', message);
ws.send(`Hello, you sent -> $message`);
);
//send immediatly a feedback to the incoming connection
ws.send('Hi there, I am a WebSocket server');
);
//start our server
server.listen(3000, () =>
console.log(`Server started on port $server.address().port :)`);
);
For the client, you can do something like this:
const ws = new WebSocket('ws://localhost:3000')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received`, message.data)
Like i have mentioned above, it is advised that you use a mature framework for websockets. Should your app be minimal and not need scaling, you can use any open source library, with socket.io being the most popular.
However, if you are talking about implementing this to be used at production level, you should know that the open source solutions do not allow for scalability, failover, message ordering etc. In that case, you’ll have to implement a realtime platform as a service tool. Ably’s Data Stream Network is one such solution that I’ve personally used for a lot of my projects.
edited Nov 15 '18 at 18:23
answered Nov 14 '18 at 8:32
ayodele samuelayodele samuel
214
214
Have tried your example but on the client side as part of your ws.onmessage console log, I am receiving the output: message received [object MessageEvent] - what is this and what should have actually returned from the server as I don't think I am getting the correct message? Furthermore, in your server code, I believe all you "ws.on/send" should actually be "WebSocket.on/send"
– tonyf
Nov 14 '18 at 14:24
This is because the message variable is an object and was displayed in a string. you would need to accessmessage.data
to get the message that was actually sent. Updated the code to show that
– ayodele samuel
Nov 15 '18 at 18:23
add a comment |
Have tried your example but on the client side as part of your ws.onmessage console log, I am receiving the output: message received [object MessageEvent] - what is this and what should have actually returned from the server as I don't think I am getting the correct message? Furthermore, in your server code, I believe all you "ws.on/send" should actually be "WebSocket.on/send"
– tonyf
Nov 14 '18 at 14:24
This is because the message variable is an object and was displayed in a string. you would need to accessmessage.data
to get the message that was actually sent. Updated the code to show that
– ayodele samuel
Nov 15 '18 at 18:23
Have tried your example but on the client side as part of your ws.onmessage console log, I am receiving the output: message received [object MessageEvent] - what is this and what should have actually returned from the server as I don't think I am getting the correct message? Furthermore, in your server code, I believe all you "ws.on/send" should actually be "WebSocket.on/send"
– tonyf
Nov 14 '18 at 14:24
Have tried your example but on the client side as part of your ws.onmessage console log, I am receiving the output: message received [object MessageEvent] - what is this and what should have actually returned from the server as I don't think I am getting the correct message? Furthermore, in your server code, I believe all you "ws.on/send" should actually be "WebSocket.on/send"
– tonyf
Nov 14 '18 at 14:24
This is because the message variable is an object and was displayed in a string. you would need to access
message.data
to get the message that was actually sent. Updated the code to show that– ayodele samuel
Nov 15 '18 at 18:23
This is because the message variable is an object and was displayed in a string. you would need to access
message.data
to get the message that was actually sent. Updated the code to show that– ayodele samuel
Nov 15 '18 at 18:23
add a comment |
Just a note, socket.io
is a backend/frontend library that uses websocket
but also has a number of fallbacks if the client browser does not support websocket. The example below works with ws
backend.
Server
const WS = require('ws')
const PORT = process.env.PORT || 8080
const wss = new WS.Server(
port: PORT
, () => console.log(`ws server live on $PORT`))
const errHandle = (err) =>
if(err) throw err
wss.on('connection', (socket) =>
console.log('something connected')
socket.send('you are connected', errHandle)
socket.on('message', (data) =>
console.log(`socket sent $data`)
socket.send('message received', errHandle)
)
)
client (browser)
(() =>
const ws = new WebSocket('ws://localhost:8080')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received $message`)
)()
edit: oh, and ws
and http
are different protocols. you will need a different server to serve your http files
add a comment |
Just a note, socket.io
is a backend/frontend library that uses websocket
but also has a number of fallbacks if the client browser does not support websocket. The example below works with ws
backend.
Server
const WS = require('ws')
const PORT = process.env.PORT || 8080
const wss = new WS.Server(
port: PORT
, () => console.log(`ws server live on $PORT`))
const errHandle = (err) =>
if(err) throw err
wss.on('connection', (socket) =>
console.log('something connected')
socket.send('you are connected', errHandle)
socket.on('message', (data) =>
console.log(`socket sent $data`)
socket.send('message received', errHandle)
)
)
client (browser)
(() =>
const ws = new WebSocket('ws://localhost:8080')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received $message`)
)()
edit: oh, and ws
and http
are different protocols. you will need a different server to serve your http files
add a comment |
Just a note, socket.io
is a backend/frontend library that uses websocket
but also has a number of fallbacks if the client browser does not support websocket. The example below works with ws
backend.
Server
const WS = require('ws')
const PORT = process.env.PORT || 8080
const wss = new WS.Server(
port: PORT
, () => console.log(`ws server live on $PORT`))
const errHandle = (err) =>
if(err) throw err
wss.on('connection', (socket) =>
console.log('something connected')
socket.send('you are connected', errHandle)
socket.on('message', (data) =>
console.log(`socket sent $data`)
socket.send('message received', errHandle)
)
)
client (browser)
(() =>
const ws = new WebSocket('ws://localhost:8080')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received $message`)
)()
edit: oh, and ws
and http
are different protocols. you will need a different server to serve your http files
Just a note, socket.io
is a backend/frontend library that uses websocket
but also has a number of fallbacks if the client browser does not support websocket. The example below works with ws
backend.
Server
const WS = require('ws')
const PORT = process.env.PORT || 8080
const wss = new WS.Server(
port: PORT
, () => console.log(`ws server live on $PORT`))
const errHandle = (err) =>
if(err) throw err
wss.on('connection', (socket) =>
console.log('something connected')
socket.send('you are connected', errHandle)
socket.on('message', (data) =>
console.log(`socket sent $data`)
socket.send('message received', errHandle)
)
)
client (browser)
(() =>
const ws = new WebSocket('ws://localhost:8080')
ws.onopen = () =>
console.log('ws opened on browser')
ws.send('hello world')
ws.onmessage = (message) =>
console.log(`message received $message`)
)()
edit: oh, and ws
and http
are different protocols. you will need a different server to serve your http files
edited Nov 14 '18 at 7:54
answered Nov 14 '18 at 7:48
pandamakespandamakes
1826
1826
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%2f53294938%2fsimple-example-on-how-to-use-websockets-between-client-and-server%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
websocket servers are listening for messages from clients (and viceversa), not the backend itself. What you want is some kind of error handling framework (depending on which lib throws the error it might implement onerror callback handlers)
– Adelin
Nov 14 '18 at 7:22
The title asks for one thing and the body asks for a completely different thing
– Adelin
Nov 14 '18 at 7:22
@Adelin Thanks for pointing that out. Have edited my original post.
– tonyf
Nov 14 '18 at 11:48