Questions tagged [ws]

ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and server JavaScript implementation.

ws: a Node.js WebSocket library

WebSockets represent a long awaited evolution in client/server technology. They allow a single TCP socket connection to be established between the client and server which allows for bi-directional messages to be distributed with little overhead resulting in a low latency connection.

ws allows to easily implement this protocol on Node applications, both for WebSocket servers and WebSocket clients. This module is not suited for browser apps, which must use the native WebSocket object; the client WebSocket capabilities are designed for Node applications with the role of a client.

Resources

Example

// index.js
const WebSocket = require('ws');

// server code
const wsServer = new WebSocket.Server({port: 8080});
wsServer.on('connection', serverSocket => {
    console.log('server: new connection');
    serverSocket.onopen = evt => console.log('server: open');
    serverSocket.onmessage = evt => console.log('server: message:', evt.data);
    serverSocket.onclose = evt => console.log('server: close');
    setTimeout(() => serverSocket.send('Hello from server'), 1000);
    setTimeout(() => serverSocket.close(), 3000);
});

// client code
const clientSocket = new WebSocket('ws://localhost:8080');
clientSocket.onopen = evt => console.log('client: open');
clientSocket.onmessage = evt => console.log('client: message:', evt.data);
clientSocket.onclose = evt => console.log('client: close');
setTimeout(() => clientSocket.send('Hello from client'), 2000);

Commented standard output of node index.js:

# t=0s
server: new connection
client: open

# t=1s
client: message: Hello from server

# t=2s
server: message: Hello from client

# t=3s
server: close
client: close
341 questions
2
votes
0 answers

ERR_SSL_SSLV3_ALERT_CERTIFICATE_UNKNOWN thrown by wss server when client tries to connect to it

recently I've been trying to create a WebSocket server (using the ws library for node.js). At first I used the ws unencrypted protocol, but then I had to switch to wss. This brought some client authentication issues. When the client (running on a…
Fosco110
  • 21
  • 3
2
votes
1 answer

Unit Test cases for websocket server "ws" library using jest

I am trying to figure out how to write unit test cases for Websocket server which is using the ws library. I did go through jest-websocket-mock but I think this is for browser based APIs and I want to test server using JEST. Basic…
Jaspreet Chhabra
  • 377
  • 3
  • 14
2
votes
1 answer

websocket subscription server not working. unable to reach subscription server graphql-ws ws

i follow the [same documentation code][1] for creating subscription server using websocket graphql, put it doesn't work with graphql-ws and ws the code worked when i removed the serverCleanup definition, and it also woks well for older subscription…
entesar
  • 929
  • 1
  • 8
  • 12
2
votes
0 answers

WebpackDevServer crashing with Error: RSV1 must be clear

In a new project that I set up, the webpack dev server initializes a websocket on my specified port and attempts to communicate reload commands etc. through the socket. The problem is, on specific ports (that I've used on past projects) the dev…
2
votes
2 answers

ReferenceError: WebSocket is not defined in client side typescript react application

I am working on a React frontend application and I am trying to set up a WebSocket connection to my Golang backend server. So far I have a simple class that is supposed to initiate a websocket connection using the native websocket object. It is…
SomeGuyFortune
  • 1,024
  • 13
  • 26
2
votes
1 answer

How to access Express-style request objects when upgrading to websocket?

Using Express + ws (websocket) libraries. "App" uses some Express middleware to add the current user to the request object, but WS gets a Node request object without the information I need. How can I pass this data to the websocket when upgrading…
jdoe8227
  • 41
  • 4
2
votes
1 answer

Invalid WebSocket frame: MASK must be clear

I'm using the ws package to connect to the poloniex web socket, using the following code: const WS = require('ws'); const ws = new WS('wss://api2.poloniex.com'); ws.on('open', () => { ws.send(JSON.stringify({ command: 'subscribe', …
cj-2307
  • 259
  • 3
  • 14
2
votes
1 answer

API Gateway Web-socket route selection not working

I have created a basic example APIGateway for websocket and integrated same lambda to all $connect $disconnect and a test route Route selection expression being $request.body.action, I am trying to send a message to my test route. But it doesn't…
2
votes
1 answer

How to save websocket session to redis using the ws library?

Newbie to this, I am using the ws library in my express node.js server to support websockets Currently I just store them to an in memory array I would like to store each session in redis How do I achieve this? This GitHub issue on their repository…
PirateApp
  • 5,433
  • 4
  • 57
  • 90
2
votes
1 answer

Having trouble with the 'ws' package (Namespace has no exported member 'Server')

I'm trying to create a WebSocket server with the ws package, but I keep getting this error: Namespace has no exported member 'Server' I've tried importing the ws package in different ways, but none work. I can't seem to figure out how to make the…
Current
  • 65
  • 1
  • 5
2
votes
1 answer

SubcribeMessage decorator doesn't trigger on event 'message'

I'm setting up a WebSocket server on my NestJS backend, and when I try subscribing on the default 'message' event type, the method handleMessage() doesn't get triggered. The listenForMessages() method however works (which is triggered after the init…
2
votes
1 answer

Nestjs wss handleConnection(socket) socket.handshake is undefined and cannot access headers to authenticate

I have a Nestjs gateway where I'm trying to run authorization logic depending on values from the headers but wherever I'm trying to access the handshake it always returns 'undefined' I'm also trying this over SSL which might be making a…
SebastianG
  • 8,563
  • 8
  • 47
  • 111
2
votes
3 answers

How do I catch ping/pong frames sent to my ws listener from the server?

The ws package for nodeJS hides incoming ping frames by default and silently responds to them with pong frames. How can I catch these incoming ping frames and take note of them?
randy
  • 156
  • 9
2
votes
1 answer

Client ws can't connect to nodejs websocket server hosted on Azure app service?

Anybody knows how to setup a websocket server nodejs (npm package ws) app service on Azure ? My ws client can't connect to the ws server... Thank you for any hint!
Bruce LANE
  • 89
  • 2
  • 11
2
votes
0 answers

Why Do People Try Reconnecting Websockets After It Closes?

I'm creating a real-time, multiplayer browser game using the ws websocket library in Javascript. I've seen multiple posts talking about the reconnection for their websockets. However, I don't understand why reconnecting the websocket is necessary.…
user8380672
  • 520
  • 1
  • 9
  • 20
1 2
3
22 23