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

Playframework JavaWS logs correct curl script but not works correctly

I am trying to send email via web service. The curl script which is logged by AhcCurlRequestLogger works fine when I paste it into terminal. But service method not work. The code does not enter wsResponse part. The java function: public…
karakale
  • 126
  • 11
0
votes
0 answers

Create routes in websockets and node/express

I am trying to somehow have some separation of my WS code in my node/express. I want to have some routes in my WS code, like the API routes that express has. I am using the ws module I guess I could use something out of the box like express-ws, but…
codebot
  • 517
  • 8
  • 29
  • 55
0
votes
2 answers

Why hook an httpserver in a websocket server in node/express?

I see a lot of examples hooking an http server during the creation of a WS server, more or less like the following var server = http.createServer(function(request, response) { // process HTTP request. Since we're writing just WebSockets //…
codebot
  • 517
  • 8
  • 29
  • 55
0
votes
1 answer

WebSocket stops working in Vue/Node application

I have a Node/Vue application. I am consuming a WebSocket from Binance, a crypto exchange. I can see the quotes on the server console as I log them, I can send them to the browser for a short period of time before the client stops logging them. …
illcrx
  • 774
  • 1
  • 12
  • 32
0
votes
2 answers

Connect to Rcon using WebSockets (ws)

I'm new to using web sockets in nodejs in my electronjs project. My goal is to connect to the connection my server is running, I have everything setup correctly, the port, password, and localhost, all seemed to work using another package. But when I…
Charles
  • 1
  • 3
0
votes
1 answer

I am trying to connect with my live server. getting Connection refused error in iOS

I am trying to connect with my live server for web socket connection. But I am getting below error in iOs. websocket is disconnected: Optional(Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo={_kCFStreamErrorCodeKey=61, …
Jignesh Kanani
  • 223
  • 1
  • 4
  • 18
0
votes
1 answer

Websocket client on the ESP8266 doesn't send data to spring-boot server

I have a simple project with ESP8266 and spring boot server. And I want to send data from ESP to the server via websocket. On ESP I`m using this library to create Stomp client: https://github.com/ukmaker/StompClient Spring Boot ws config: …
0
votes
1 answer

Send an array from Python to PHP

I am trying to consume with Python a WSDL created in PHP. Fragment of the Service: require_once('lib/nusoap.php'); date_default_timezone_set('America/Mexico_City'); function Sum($numbers){ return…
0
votes
1 answer

Cannot connect remotely to WebSocket server with React App, running in Expo

I have been unsuccessful in trying to make a WebSocket connection between a React Native app client (running in Expo) and a NodeJs server, using the 'ws' websocket library on both sides. I have set up the WebSocket server, which I can successfully…
Alex Gourlay
  • 365
  • 1
  • 12
0
votes
0 answers

How can i wrap a WebSocket server class with Node.js, ws, and es6-class?

I am a beginner of Node.js. Recently i would like to build a modulize system based on es6-class. I am trying to wrap ws module to a class, and here is the prototype of my code: const WebSocket = require('ws') export default class WebSocketServer { …
Ikki
  • 1
  • 3
0
votes
1 answer

express-ws: How to avoid typing the port on the request url?

I've successfully created a WS Server using Node.js and wxpress-ws. The problem is that I don't know which port to use to not type it in the request URL. So, instead of using ws://mysite.com::xxxx/, I need to be able to type only…
Bruno Albuquerque
  • 597
  • 1
  • 7
  • 21
0
votes
2 answers

Express server not responding to reqs

After sending a bunch of POST/GET reqs to the server my server stops responding and in the network tab of the console all the reqs are labeled as "pending". I have logged timestamps during the responses but they go fast and they just don't run once…
0
votes
0 answers

rewriting existing websocket to use with typescript

having trouble converting existing ws to typescript from javascript. Cant understand what is the return type of async socketHost functions and why i cant access them via 'this' keyword here's the code: const config = { production: { …
dobbey
  • 105
  • 1
  • 7
0
votes
0 answers

Make Node.js websocket client run indefinitely

I'm using ws library to handle Websocket connections client-side. Here's example from ws docs (https://github.com/websockets/ws#usage-examples): const WebSocket = require('ws'); const ws = new WebSocket('ws://www.host.com/path'); ws.on('open',…
userQWERTY
  • 501
  • 1
  • 5
  • 12
0
votes
0 answers

Secure webSocket on local network for android browsers

I am trying to run a web on my local network that connects to the server through a websocket. When I test it from the same machine that is serving the web (localhost) it works fine but when I try to access the web from my android phone with chrome I…
1 2 3
22
23