According to the npm docs :
ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and server implementation.
So I have installed it
npm install ws
and tried a little test on Node js file :
const Websocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path');
but I receive an error :
ReferenceError : WebSocket is not defined
What am i doing wrong please ?
EDIT :
It was just a typing error.
I used
const Websocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path');
But I have to use :
const WebSocket = require('ws'); // WebSocket with camelCase...
const ws = new WebSocket('ws://www.host.com/path');
Your using the client logic on the server, try
const WebSocket = require('ws').Server;
const ws = new WebSocket({ port: 8080 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With