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

mocha test not exiting when using websockets, even with websocket.close/termintae

I'm trying to test my websocket server, by opening a websocket client in my mocha test file, connection to the ws server and awaiting response. I send an http request to the ws server, and then a message is sent via websocket to the client, where I…
0
votes
1 answer

How to maintain a valid order book in kraken exchange with node,js

How's it going? I got the example order book code in python (https://support.kraken.com/hc/en-us/articles/360027677512-Example-order-book-code-Python-) and translate it to javascript to run in node. But the book is wrong, it doesn't remove all old…
Luiz
  • 1
  • 1
0
votes
0 answers

WCF: Rename the xsi:type in XML

I am developing a SOAP service using WCF in C# to a specific set of requirements. I have to develop one method with that accept multiple types, and I achieved that. However, the requirements state the type attribute has to be named "BaseType". I…
Mark
  • 148
  • 3
  • 14
0
votes
1 answer

Able to redirect https to http but not able to redirect wss to ws

I am working with web sockets. In apache I am able to redirect from https to http by doing below setting in configuration file: # Common SSL Config ServerName ec2-13-52-248-221.us-west-1.compute.amazonaws.com …
Parag Jain
  • 612
  • 2
  • 14
  • 31
0
votes
1 answer

C# Service HttpClient reponse null

I have gone through all the possible posts and I have not found any solution that works for me (There is no more to see the mess in imports), please help. :( I am building a windows service in c# and I need to call a web service every X time and…
0
votes
0 answers

how can i send and receive messages separately with nodejs ws

i got this code example from npm that im using for one of my projects : const WebSocket = require('ws'); const ws = new WebSocket('ws://www.host.com/path'); ws.on('open', function open() { ws.send('something'); }); ws.on('message', function…
Assaf Schwartz
  • 119
  • 1
  • 7
0
votes
0 answers

WebSocket connection never fully established when traffic is routed through Cloudflare

I use a service called glitch.me to host my Node.js project, and they only allow you to open one port internally. At the moment, my Node.js server can only handle HTTP requests, but glitch.me proxies my traffic for me, effectively allowing the…
emil
  • 36
  • 3
0
votes
0 answers

Websocket consecutive send() on different routes

As title says: I'm trying to set up websocket connection like: var socket = new WebSocket('ws://localhost:8080/admin'); /admin will return list of sessions. Then for each session I need to send more data but to /admin/${sessionX} Is this even…
mirkobrankovic
  • 2,389
  • 1
  • 21
  • 24
0
votes
1 answer

Use ReactPHP Socket to open a ws:// socket

I've been trying to get ReactPHP socket up and running for a bit now, once up, I can telnet to it on the specified port but I cannot use websocat or any js lib to connect via ws:// protocol. Any help would be appreciated. $loop =…
Farid Anthony
  • 11
  • 1
  • 1
0
votes
0 answers

Websocket .close() is not recognised

Im trying to close a websocket from an npm package that connects to a third party service. I instantiate the ws by doing: ws = BitMexPlus({ apiKeyID: rows[i].bot_key.apiKeyID, apiKeySecret:…
lucas rodriguez
  • 980
  • 2
  • 8
  • 20
0
votes
0 answers

Socket ping error using the ws library (nodejs)

I've successfully implementented the ws package for nodejs to set up a websocket server. Everythings work fine except when I try to check the socket availability as per…
Jérôme Morlon
  • 191
  • 1
  • 7
0
votes
2 answers

Gatling: WebSocketHandshakeException: Invalid handshake response getStatus: 400 Bad Request

I'm trying to open ws connection, but get the error: val openConnection = exec( ws("Connect -> WS").wsName("user").connect("wss://socket.develop.test.com?access_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9")) Then, I get: 09:49:53.343 [DEBUG]…
Andrey
  • 53
  • 1
  • 2
  • 11
0
votes
1 answer

Variable inside websocket callback not updating

I am writing a Koa middleware that, on each request to the server, analyzes some input from a file on the server, and sends it over the websocket (using https://www.npmjs.com/package/ws) to the client. This all works as expected, except for one…
0
votes
0 answers

Websocket broken but still receive ping message

I'm using nodejs ws module to implement websockets. The socket is broken but I continue to receive ping messages. I've configured heartbeat function to auto-close and restart when broken, but because the ping message keeps coming the socket restart…
Pumpkin Pie
  • 510
  • 1
  • 4
  • 15
0
votes
0 answers

Arduino websocket needs updating using node "express" and "ws" made 6 years ago

Six years ago, (used ws version 0.4.32 now we use 7.2.1 ), I had a great little websocket that ran on http://repl.it and connected a Particle Photon to a webpage with very fast interaction < 10 ms per command. I need to now update my code for…
Rocksetta
  • 35
  • 5