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
19
votes
4 answers

How to create/join chat room using ws (Websocket) package in node js

I am using ws package in server side and I want the client to create/join rooms in server socket. And to remove them from a created room when they are no longer connected. PS: I don't want to use socketIo.
11
votes
3 answers

How can I make a rust websocket client?

I've tried using different libraries and different implementations but I haven't been able to get a working WebSocket client/ listener in rust. I tried writing a handler: extern crate ws; use ws::{connect, listen, Handler, Sender, Handshake,…
Loading
  • 426
  • 1
  • 5
  • 11
10
votes
5 answers

WebSocketClient.js:16 WebSocket connection to 'ws://localhost:3000/ws' failed: React, Docker, NGINX

Here's the issue... when I start a React app locally as npm start. I don't have a ws failed connection. If I start NGINX and React servers within Docker containers I constantly get WebSocketClient.js:16 WebSocket connection to…
hazartilirot
  • 195
  • 1
  • 2
  • 11
10
votes
4 answers

Why is received websocket data coming out as a buffer?

I'm trying to do a very basic websocket, but i dont understand why I'm not getting a string back. I'm using the ws module from npm for the server. https://github.com/websockets/ws client: let socket = new WebSocket('wss://upload.lospec.com'); …
stackers
  • 2,701
  • 4
  • 34
  • 66
10
votes
2 answers

How to stop Jest from hanging when testing WebSockets?

I created an app in node.js which gives me a WebSocket interface using the 'ws' package from NPM on the server. Now I want to test this interface with Jest. The test runs successful but Jest does not exit and gives me the error: Jest did not exit…
FleMo
  • 539
  • 6
  • 20
8
votes
3 answers

GraphQL Subscriptions vs socket.io

I'm trying to make real-time application based on websocket and got two options. One is socket.io and the other is GraphQL Subscriptions. But it was hard to find comparison of those. What can be standard to choose one of them and is there any…
lcpnine
  • 477
  • 1
  • 7
  • 16
8
votes
1 answer

Sending a WebSocket message from a POST request handler in express

I have an Express server that listens for webhook events from an external API. When it received these events, I want the handler for that http request to send a message to a WebSocket client. Here is some basic code to illustrate what I mean The…
Gautam Jethwani
  • 121
  • 1
  • 2
  • 10
8
votes
0 answers

How to handle 'upgrade' events manually with Socket.io v2

I'm transitioning a pre-existing project from Socket.io (socket-io / socketio) to plain Websockets and I'd like to run both side-by-side during the transition. What I'd Expect To Do 'use strict'; var app = require('express')(); var server =…
coolaj86
  • 74,004
  • 20
  • 105
  • 125
8
votes
1 answer

Client-side websocket certificate in NodeJS

I have a NodeJS websocket client app, using ws https://www.npmjs.com/package/ws - this NodeJS app connects as a client to a websocket server. I can use HTTPS by specifying wss:// as the protocol. How can I make the TLS connection use a client…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
7
votes
1 answer

How can I make SocketIO more performant?

We used SocketIO quite extensively in Etherpad(since very early on) and we're really grateful for all of the efforts of the team for providing such a useful thingy :) Etherpad is a nodejs project. My problem with SocketIO is probably due to me…
John McLear
  • 764
  • 1
  • 10
  • 20
6
votes
0 answers

How do rewrites in NextJs for WebSocket?

How to use http proxy for web socket with next js rewrites. The destination does not start with /, http://, or https:// for the flowing example. { "source":"/websocket", "destination":"wss://some.domine.com/websocket" } How to solve it?
Timur Zakirov
  • 381
  • 2
  • 8
6
votes
1 answer

How to give certificate to Java Websocket?

Forgive me for the newb question, but I am confused and obviously not understanding the fundamentals or explanations of how to use a Websocket server hosted over HTTPS. Everything I find online leads me to have more questions than answers. I have a…
mcool
  • 457
  • 4
  • 29
6
votes
1 answer

NestJS Websocket Gateway: using namespaces with the WS adapter

I am working on implementing a Websocket gateway in NestJS 7, as shown in the docs. I use the WS adapter. const app = await NestFactory.create(ApplicationModule); app.useWebSocketAdapter(new WsAdapter(app)); The problem I have is that the gateway…
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
6
votes
2 answers

Nodejs ws module: Heartbeat in TypeScript

I'm currently working on getting a WebSocket server running on NodeJs with TypeScript. As a WebSocket server implementation, I am using ws. Along with that I use the @types/ws package for the typings. I want the server to periodically send out a…
D. Kaiser
  • 159
  • 1
  • 9
5
votes
1 answer

Where do I add this websocket code in the new nuxt.js setup since it does not have server?

I am using the new version of Nuxt which does not come with a server folder In the old version, you had a server folder and an index.js which contained the app I want to add the websocket WS library with the new version of NuxtJS The library…
PirateApp
  • 5,433
  • 4
  • 57
  • 90
1
2 3
22 23