Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

websocket

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

websocket

Websocket Client & Server Library implementing the WebSocket protocol as specified in RFC 6455.

  • 1.0.35
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
759K
decreased by-13.62%
Maintainers
1
Weekly downloads
 
Created

What is websocket?

The 'websocket' npm package provides a WebSocket server and client for Node.js, allowing for real-time, bidirectional communication between a client and server over a single, long-lived connection.

What are websocket's main functionalities?

WebSocket Server

This code sets up a basic WebSocket server using the 'websocket' package. It listens for incoming WebSocket connections, accepts them, and allows for message exchange between the server and connected clients.

const WebSocketServer = require('websocket').server;
const http = require('http');

const server = http.createServer((request, response) => {
  response.writeHead(404);
  response.end();
});
server.listen(8080, () => {
  console.log('Server is listening on port 8080');
});

const wsServer = new WebSocketServer({
  httpServer: server
});

wsServer.on('request', (request) => {
  const connection = request.accept(null, request.origin);
  console.log('Connection accepted.');

  connection.on('message', (message) => {
    if (message.type === 'utf8') {
      console.log('Received Message: ' + message.utf8Data);
      connection.sendUTF('Hello from server!');
    }
  });

  connection.on('close', (reasonCode, description) => {
    console.log('Peer ' + connection.remoteAddress + ' disconnected.');
  });
});

WebSocket Client

This code demonstrates how to create a WebSocket client using the 'websocket' package. The client connects to a WebSocket server, handles connection events, and sends random numbers to the server at regular intervals.

const WebSocketClient = require('websocket').client;

const client = new WebSocketClient();

client.on('connectFailed', (error) => {
  console.log('Connect Error: ' + error.toString());
});

client.on('connect', (connection) => {
  console.log('WebSocket Client Connected');
  connection.on('error', (error) => {
    console.log('Connection Error: ' + error.toString());
  });
  connection.on('close', () => {
    console.log('Connection Closed');
  });
  connection.on('message', (message) => {
    if (message.type === 'utf8') {
      console.log('Received: ' + message.utf8Data);
    }
  });

  function sendNumber() {
    if (connection.connected) {
      const number = Math.round(Math.random() * 0xFFFFFF);
      connection.sendUTF(number.toString());
      setTimeout(sendNumber, 1000);
    }
  }
  sendNumber();
});

client.connect('ws://localhost:8080/', 'echo-protocol');

Other packages similar to websocket

Keywords

FAQs

Package last updated on 12 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc