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

SOAP XSD Root Element Issue

Whenever I create my XSD element name: Ansh, it(wsdl) doesnt get loaded in my SOAPUI(Soapui doesnt create operation for this request) bu when i change my element name as: AnshRequest, request template is generated.
Ansh Hora
  • 31
  • 3
2
votes
1 answer

Error ServerMiddleware should expose a handle nuxt

The new nuxt.js setup does not come with a server folder You create an API folder and put a file inside which exposes the server I am trying to use websockets using the ws library to parse user session and getting this error Here is my code for…
PirateApp
  • 5,433
  • 4
  • 57
  • 90
2
votes
0 answers

How to create a page that shows cursor position of all users connected? Node.js, ws, express

I want to create a simple page where I would display mouse coordinates of all the users connected. I got as far as showing mouse coordinates of a single connected client. Sending the mouse coordinates to the server. Sending the data back. I don't…
neven
  • 33
  • 5
2
votes
1 answer

Websocket connection error: returns 101, but does not upgrade

I am setting up some websockets using ws library. I am struggling to set up authorisation using a handshake. I have added a route to our server to upgrade to a websocket connection like so: .get( '/chat', authorisationFunction, …
tal
  • 35
  • 1
  • 1
  • 7
2
votes
1 answer

Prevent stopping of Node app while Windows 10 desktop switches to lock screen

I'm implementing a Node application using Electron that communicates with a websocket server. The app runs smoothly as long as the user is logged in. If the user is inactive for some time, the lock screen of the Windows 10 system shows up. The…
MadMaxAPP
  • 1,035
  • 2
  • 16
  • 39
2
votes
0 answers

How to Stream utf8 over WebSockets in NodeJS ws?

I am not sure if this is a bug or not. According to websocket/ws documentation you can use NodeJS streams. However, my browser only receives Binary messages when doing this, despite setting encoding to utf8: (new WebSocket.Server({port:…
marknadal
  • 7,534
  • 4
  • 25
  • 22
2
votes
0 answers

Apollo graphql subscriptions, using the same endpoint for the graphql server and websocket endpoint

I was just wondering if there was any performance decrease or any disadvantage in using the same endpoint for the graphql endpoint and also for the WebSocket. You can see the sample code below. import express = require("express"); import {…
Mikias Abebe
  • 149
  • 2
  • 6
2
votes
1 answer

"Where" are Websocket servers hosted on Heroku?

I'm trying to host a Websocket server on my heroku app. There already is a GraphQL server running, but I don't think it's the cause of my problem. So my server is started like this const wss = new ws.Server({ port: port }, () =>…
Jacques
  • 140
  • 1
  • 10
2
votes
1 answer

Multiple socket.io servers sharing a single HTTP/S server

With ws, Node.js WebSocket library, it is possible to have multiple servers sharing a single HTTP/S server. Is it possible to do the same with socket.io? I need to have two WebSocket servers on the same HTTP server, one for socket.io and another one…
Mario
  • 1,213
  • 2
  • 12
  • 37
2
votes
1 answer

Eclipse gets "IWAB0014E Unexpected exception occurred." when I'm trying create a soap client

I'm trying to create a soap web service client and when I go to File > New > Other > WebServices > WebServiceClient. I'm getting this error: IWAB0014E Unexpected exception occurred. loader constraint violation: when resolving overridden method…
hamidreza75
  • 567
  • 6
  • 15
2
votes
2 answers

Why do we pass an http server to a websocket instance in javascript on nodejs?

What exactly does code something like var WebSocketServer = require("ws").Server, express = require("express"), http = require("http"), app = express(), server = http.createServer(app); var wss = new…
hawexp
  • 75
  • 1
  • 6
2
votes
2 answers

nodejs ws library -- how to prevent brute force attack (both for passwords and for crashing)

I'm trying to make an application with NodeJS which heavily depends on users connecting to it via the typcial WebSocket protocol from their browsers, I'm using the ws library for this in nodeJS. The actual application works fine, and I'm able to…
2
votes
1 answer

Endpoint cannot be resolved to a type (@Endpoint in ws)- Eclipse compilation error

This is some what strange or may be i am missing something. I am publishing an SOAP endpoint with Spring Boot application. Below is pom.xml file
Gunwant
  • 949
  • 1
  • 14
  • 29
2
votes
1 answer

Does NodeJS WebSockets (ws) module implement backpressure?

I'm implementing a WebSockets server on NodeJS using the ws module. The server should send once per minute an update to all clients. I have this already implemented, but I have some concerns about its functionality in conditions where client…
juhist
  • 4,210
  • 16
  • 33
1
vote
2 answers

How to broadcast a message to all clients with different worker in ws and express?

In my express app, I am using clusters and ws package to run project with a socket connection. When a client connect to socket one of workers handle the connection (For example worker with id 1). But when another client connected, then another…
Alirezakvr
  • 75
  • 1
  • 11