How to use the WebSocket function from ws
Find comprehensive JavaScript ws.WebSocket code examples handpicked from public code repositorys.
ws.WebSocket is a class provided by the ws library for creating WebSocket connections between a client and a server.
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
if (!this.config.sdport || this.config.sdport < 1 || this.config.sdport > 65535) { this.config.sdport = this.config.port } // Connect to Stage Display websocket of ProPresenter this.sdsocket = new WebSocket('ws://' + this.config.host + ':' + this.config.sdport + '/stagedisplay') this.sdsocket.on('open', () => { this.log('info', 'Opened websocket to ProPresenter stage display: ' + this.config.host + ':' + this.config.sdport) this.sdsocket.send(
+ 5 other calls in file
GitHub: kamiya10/kami-bot
2 3 4 5 6 7 8 9 10 11 12 13 14 15
const logger = require("./logger"); module.exports = connect; function connect(client, retryTimeout) { let ws = new WebSocket("wss://exptech.com.tw/api", { handshakeTimeout: 3000 }); let ping, heartbeat; ws.on("close", () => {
How does ws.WebSocket work?
ws.WebSocket
is a class in the ws
library that allows for creating WebSocket connections between a client and a server, enabling real-time, bidirectional communication between them using a message-based protocol over a single, long-lived TCP connection.
The ws.WebSocket
class provides methods for opening, closing, and sending messages over a WebSocket connection, as well as events for handling incoming messages, errors, and the state of the connection. The class can be instantiated with the URL of the WebSocket server and optional configuration options. Once a WebSocket connection is established, data can be sent and received in both directions in real-time.
76 77 78 79 80 81 82 83 84 85
bybitSpotSocket .on("open", () => { console.log("bybit spot socket connected"); bybitSpotSocket.send(JSON.stringify(openMsg)); setInterval(() => { if (bybitSpotSocket.readyState === ws_1.WebSocket.OPEN) { bybitSpotSocket.send('{"op": "ping"}'); } }, INTERVAL_MS); })
+ 23 other calls in file
27 28 29 30 31 32 33 34 35 36
// // The certificate used in this example is self-signed so `rejectUnauthorized` // is set to `false`. // console.log(server.address().port); const ws = new WebSocket(`wss://localhost:${server.address().port}`, { rejectUnauthorized: false }); ws.on('error', console.error);
+ 3 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
const WebSocket = require("ws"); const ws = new WebSocket("ws://localhost:8080"); ws.on("open", function () { console.log("Connected to WebSocket server"); // Send a message to the server ws.send("Hello, server!"); }); ws.on("message", function (message) { console.log(`Received message: ${message}`); }); ws.on("close", function () { console.log("Disconnected from WebSocket server"); });
In this example, we create a new WebSocket instance and connect it to a server running on localhost at port 8080. We then listen for events on the WebSocket instance, such as the open event which is triggered when the connection is established. We also send a message to the server using the send() method, and listen for messages from the server using the message event. Finally, we handle the close event, which is triggered when the connection is closed.
21 22 23 24 25 26 27 28 29 30
wss.on("connection", ws => { wss.clients.add(ws); ws.on("error", console.error); ws.on("message", message => { wss.clients.forEach(wsClient => { if (wsClient !== ws && wsClient.readyState === ws_1.WebSocket.OPEN) { wsClient.send(message.toString()); } }); });
8 9 10 11 12 13 14 15 16 17
const latestIndexPrice = { BTC: 0, ETH: 0 }; function startBitcomWS() { bitcomSocket = new ws_1.WebSocket(common_1.bitcom.wsURL); const idxPriceSubMsg = { type: "subscribe", pairs: [ "BTC-USDT",
24 25 26 27 28 29 30 31 32 33
okexSocket .on("open", () => { console.log("okex websocket connected"); okexSocket.send(JSON.stringify(openMsg)); setInterval(() => { if (okexSocket.readyState === ws_1.WebSocket.OPEN) { okexSocket.send("ping"); } }, INTERVAL_MS); })
7 8 9 10 11 12 13 14 15 16
BTC: 0, ETH: 0, SOL: 0 }; function startBinanceWS() { binanceSocket = new ws_1.WebSocket(common_1.binance.wsURL); const openMsg = { method: "SUBSCRIBE", params: [ "BTC@trade",
5 6 7 8 9 10 11 12 13 14
const blockTrade_1 = require("../../resource/blockTrade.js"); const contractsTraded_1 = require("../../resource/contractsTraded.js"); const common_1 = require("../../common.js"); let deribitSocket; function startDeribitWS() { deribitSocket = new ws_1.WebSocket(common_1.deribit.wsURL); const openMsg = { jsonRpc: "2.0", id: 42, method: "public/subscribe",