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
0
votes
0 answers

How can I store webSocket client obj on redis database

I would like to presist the websocket ws client connections on server restart. It seems like the fastest way to do it is using redis. I'm not really sure how I can go about that, since redis doesn't accept objects (i.e. ws clients). How would I…
Jessica
  • 9,379
  • 14
  • 65
  • 136
0
votes
2 answers

Authenticating using ws WebSocket

I'm using ws webSocket (can't use socket.io) and I'm a bit new to it. I know how it works, but don't fully get a couple of things. How can I authenticate using jwt? The docs say that using the upgrade event is the correct way to go about it, but…
Jessica
  • 9,379
  • 14
  • 65
  • 136
0
votes
0 answers

C# WebSockets-Sharp WebSocketSharpException: "The header of frame cannot be read from the stream"

I have a visual C# application that connects via WebSocket to my remote Node.js server that is deployed using Heroku. The server uses the npm WebSocket module "ws" to create a WebSocket-Server. The C# client application uses the WebSocketSharp…
Phillip Schulze
  • 118
  • 2
  • 8
0
votes
1 answer

Node.js app in a Nginx subfolder with WSS connection to the same server

After many years managing my web projects on an Apache server, I had to move them to a new server using Nginx. I've succeeded in migrating all of them except the one that uses websockets. The project is a web-based minesweeper using websockets to…
Desmu
  • 1
  • 3
0
votes
0 answers

ws library connection doubled

I have a problem on connection , I have a express server and create a ws instance passing the express server instance , when I connect, the connection was successful and try to send a message I can receive message once, and when I close the…
Rey
  • 1
  • 2
0
votes
1 answer

Java service using Jersey won't deploy to Jboss

I'm trying to build a Java service that other services could call. This service is not a WS, but is calling a RestfulWS. I'm really just building a wrapper around this call. This would find the correct data it needs, set up the JSON for the…
Max Power
  • 65
  • 1
  • 12
0
votes
1 answer

How "img" tag showing running video (very curious to know)

no response img tag is showing live video from my mobile camera but response is empty. So how this is getting data continiously ? I have attached a video footage of this please check that to see exact thing happening. thank you ! img tag video…
Amir Rahman
  • 1,076
  • 1
  • 6
  • 15
0
votes
1 answer

Does WCF with TransportWithMessageCredential use SSL?

I'm configuring a WCF service in the intranet between a client and a server. I've set it up for wsHttpBinding with TransportWithMessageCredentia without certificate authentication. Am I correct that service now use ssl/tls and encrypts the…
wandapong
  • 59
  • 5
0
votes
1 answer

How to limit number of Websocket connection using websockets/ws lib?

I want to put a hard limit on the max number of WS connections to max 100. I'm writing a node.js server where I should not allow more than 100 connections. How to limit the number of Websocket connection without touching system ulimit?
Somnath
  • 3,247
  • 4
  • 28
  • 42
0
votes
1 answer

Send message via a certain channel in Node.js ws websockets

I want to create a bidirectional communication between a frontend and a backend using websockets. I am using Node.js ws. Using wss.clients.forEach(function each(client) { client.send("This is a message") }) I am able to send a message to…
Skusku
  • 558
  • 5
  • 11
0
votes
1 answer

How can we catch the upgrade http request from Nestjs Gateway (websockets)

In order to implement manually an authentication process with websockets in NestJS, I'm using a NestJS Gateway but have troubles setting up a handler for the "upgrade" http request that is sent during the handshake before the connection of the…
zenbeni
  • 7,019
  • 3
  • 29
  • 60
0
votes
1 answer

java spring ws: webServiceTemplate.marshalSendAndReceive sign document before sending

I'm trying to sign the document request before sending it, but when retrieving request document through Document doc = soapMessage.getSOAPPart().getEnvelope().getOwnerDocument(); and passing it to sign method which will sign it and change the…
fpeposhi
  • 31
  • 5
0
votes
1 answer

Get filtered JSON value Scala Play

I work with scala play and I use WS to make get a response from an URL. My JSON example : [ { "object": "001", "object-description": "MODEL", "criterion": "TW3", "criterion-description": "MODELE X07" }, { "object": "002", …
Robert25
  • 103
  • 6
0
votes
1 answer

Websockets inside a PM2 cluster, ok in production?

Before going to production, we want to make sure that this is an "as expected behavior". I have conducted an experiment by laucnhing 4 child processes using a PM2 cluster (I have 4 cores on my machine). Which means there were 4 websocket processes…
sofasurfa
  • 21
  • 4
0
votes
1 answer

I need a function to detect how many connections are coming from the same user-agent

Ok so people keep botting my multi-ogar edited server(which is like an agar.io private server), and I noticed that they all use the same user-agent. How can I use ws to detect how many connections are coming from the same user agent so I can block…
Coll y
  • 83
  • 1
  • 3
  • 11