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
5
votes
1 answer

Connect to websocket with cloudflare protection on python

The essence of the problem is that I used to connect to websocket by sending Origin, User-Agent, Cookies and the connection worked, now the domain owner decided to change it to the domain of the websocket and put cloudflare protection there, after…
4
votes
0 answers

Websocket keeps responding with "403 Forbidden" despite ALL the headers ARE correct

Well, I am trying to connect to this websocket ws://rustypot.com/socket.io/?EIO=4&transport=websocket but keep getting 403 forbidden error. I tried both connecting using my NodeJS backend and Postman with the same result. So, when you encounter such…
My Man
  • 63
  • 1
  • 5
4
votes
1 answer

Send a custom event with ws

I am trying to listen to a custom event on the client with native websockets and with ws on the back end. I first create a pool of connected clients. import express from 'express' import http from 'http' import WebSocket from 'ws' import { v4 as…
Álvaro
  • 2,255
  • 1
  • 22
  • 48
4
votes
2 answers

WebSocket Closes with Protocol Error 1002

I m implementing WebSocket messages command-line client. I have checked that this error corresponds to the problem with the protocol. I upgraded ws to the newest 7.4.1. At backend I use Spring Boot Websockets at version 2.3.4.RELEASE. The 2 main…
Hyphen
  • 500
  • 1
  • 5
  • 15
3
votes
2 answers

Error setting up wss server: works on localhost, but not with ip address

I'm having issues setting up a wss server (Secure Websocket Server) in node.js. When we run the server and test it using an online websocket tester and connect to wss://localhost:8888 it works. But when we connect to wss://my_ip:8888 (ip found with…
Daan Seuntjens
  • 880
  • 1
  • 18
  • 37
3
votes
1 answer

WebSockets with ws: Client closed or server closed?

In the ws Node.js WebSocket library, regarding the event socket.on('close', ...), is there any direct way (i. e. by inspecting the event) to determine if the client has closed the socket OR the server closed it for this client? Even when the server…
Amaterasu
  • 153
  • 10
3
votes
1 answer

I'm having trouble connecting websocket

I create a server using the SocketTest v 3.0.0 program. I tried to connect with JavaScript to my own server, When undefined appears in the console, I get a message from a javaScript client; Message is; GET / HTTP/1.1 Host:…
ORHAN ERDAY
  • 1,020
  • 8
  • 31
3
votes
1 answer

Websockets on Node-server hosted in Azure fails

I have a barebone simple example that works locally but not in Azure. My original question was a bit specific to an cors-error, that i have now worked around, and therefor edited away that part. But i still cant manage to make WS work on azure. And…
kirkegaard
  • 1,058
  • 2
  • 12
  • 32
3
votes
1 answer

How to send error before destroying socket nodejs socket

I'm using the ws library to create a websocket server on nodejs. the code I'm using: const express = require('express'); const app = express(); const WebSocket = require('ws'); const server = http.createServer(app); const wss = new…
Sri
  • 53
  • 5
3
votes
0 answers

publish message to web socket from route controller _ express + nodejs + webSocket

I want to know is it possible access to ws instance from router controller level? this is my entrypoint : // app.js // .... app configs const server = createServer(app); const websocket = new WebSocketServer({ httpServer: server…
ShayanJZ
  • 61
  • 4
3
votes
2 answers

Error connecting to websockets NodeJS Express server

I have an express server with websockets configured: /tmp/ws-test $ cat httpServer.js const express = require('express'); const WebSocket = require('ws'); const app = express(); const port = 8080; app.get('/', function(req, res, next) { return…
vy218
  • 707
  • 1
  • 8
  • 15
2
votes
1 answer

migrate import javax.xml.datatype.XMLGregorianCalendar; jdk11 to jakarta in jdk17

I have an application that I must migrate from jdk11 to jdk17 and when trying to generate my soap classes they are generated with the following import javax.xml.datatype.XMLGregorianCalendar; in jdk17 javax it becomes jakarta but I can't find the…
Jose Guerra
  • 65
  • 2
  • 11
2
votes
1 answer

How to setup a WebSocket server inside SvelteKit using the ws package?

I'm trying to create a simple WebSocket server that will run in a SvelteKit application. I found this tutorial online which shows how to do it using Socket.io, however I would like to use the ws module instead. This is the vite.config.ts file that…
OOPS Studio
  • 732
  • 1
  • 8
  • 25
2
votes
0 answers

Error: Parse Error: Invalid method encountered (ws)

I've recently had an issue with the ws library in node.js, where i get the HPE_INVALID_METHOD error code. The entire error that is logged while using ws.on("error") is : [Error: Parse Error: Invalid method encountered] { bytesParsed: 0, code:…
worte
  • 23
  • 3
2
votes
1 answer

WSS wrong version number - NodeJS server to Flutter app

I just set up an HTTP server (using Socket.IO 4.5.2) and managed to connect to my Flutter app (using Socket IO Client 2.0.0). This works all fine when I connect to my server via http://123.456.789.01:2345/. Now, I want to secure the connection a bit…
M Zeinstra
  • 1,931
  • 4
  • 17
  • 46
1
2
3
22 23