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

mock-socket

Package Overview
Dependencies
Maintainers
2
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-socket

Javascript mocking library for websockets and socket.io

  • 9.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is mock-socket?

The mock-socket npm package is designed to mock WebSocket and Server-Sent Events (SSE) in JavaScript applications. It is particularly useful for testing and development purposes, allowing developers to simulate WebSocket connections and SSE without needing a real server.

What are mock-socket's main functionalities?

Mock WebSocket

This feature allows you to create a mock WebSocket server and client. The server listens for connections and messages, and the client can send and receive messages. This is useful for testing WebSocket interactions without needing a real server.

const { WebSocket, Server } = require('mock-socket');

const mockServer = new Server('ws://localhost:8080');

mockServer.on('connection', socket => {
  socket.on('message', data => {
    console.log('Received message from client:', data);
    socket.send('Hello from server');
  });
});

const client = new WebSocket('ws://localhost:8080');

client.onopen = () => {
  client.send('Hello from client');
};

client.onmessage = event => {
  console.log('Received message from server:', event.data);
};

Mock Server-Sent Events (SSE)

This feature allows you to create a mock Server-Sent Events (SSE) server and client. The server can send messages to the client, which can listen for these messages. This is useful for testing SSE interactions without needing a real server.

const { EventSource, Server } = require('mock-socket');

const mockServer = new Server('http://localhost:8080');

mockServer.on('connection', socket => {
  socket.send('Hello from server');
});

const eventSource = new EventSource('http://localhost:8080');

eventSource.onmessage = event => {
  console.log('Received message from server:', event.data);
};

Other packages similar to mock-socket

Keywords

FAQs

Package last updated on 11 Sep 2023

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