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

websocket-driver

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

websocket-driver

WebSocket protocol handler with pluggable I/O

  • 0.3.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15M
increased by4.4%
Maintainers
1
Weekly downloads
 
Created

What is websocket-driver?

The websocket-driver npm package provides a low-level interface for implementing WebSocket clients and servers according to the WebSocket protocol. It is designed to work in any JavaScript environment, including Node.js and browsers, making it versatile for developing real-time, bidirectional communication applications.

What are websocket-driver's main functionalities?

Creating a WebSocket server

This code sample demonstrates how to create a WebSocket server that listens for connections, receives messages from clients, and sends a response back. It uses the `websocket-driver` package to handle the WebSocket protocol.

const http = require('http');
const WebSocketDriver = require('websocket-driver');

const server = http.createServer();

server.on('upgrade', function(request, socket, body) {
  if (!WebSocketDriver.isWebSocket(request)) return;

  var driver = WebSocketDriver.http(request);

  driver.on('connect', function() {
    driver.start();
  });

  driver.on('message', function(e) {
    console.log('Received message:', e.data);
    driver.text('Hello from server');
  });

  driver.on('close', function(e) {
    console.log('Client disconnected');
  });

  driver.parse(body);
});

server.listen(3000);

Creating a WebSocket client

This code sample shows how to create a WebSocket client that connects to a server, sends a message, and handles incoming messages and the closing of the connection. It utilizes the `websocket-driver` package for managing the WebSocket connection.

const WebSocketDriver = require('websocket-driver');

const driver = WebSocketDriver.client('ws://www.example.com/socket');

driver.on('open', function() {
  console.log('Connection opened');
  driver.text('Hello from client');
});

driver.on('message', function(e) {
  console.log('Received message:', e.data);
});

driver.on('close', function(e) {
  console.log('Connection closed');
});

driver.start();

Other packages similar to websocket-driver

Keywords

FAQs

Package last updated on 08 May 2014

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