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

simple-websockets

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-websockets

Simple websocket server and client with callback-based API.

  • 0.1.0
  • Source
  • PyPI
  • Socket score

Maintainers
1

simple-websockets

Simple websocket server and client with callback-based API.

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install simple-websockets

Usage

Server

Here is a example of websocket echo server:

from simplews import SimpleWSServer

server = SimpleWSServer(host="",
                        port=8765,
                        print_details=True, # Print basic logs
                        print_messages=True) # Print every message received

@server.on_connect
async def on_connect(ws): # called when a client connects
    print(f'Client {ws.remote_address} connected')

@server.on_message
async def on_message(ws, message): # called when a message is received from a client
    await ws.send(message) # send the message back to the client

@server.on_close
async def on_close(ws): # called when a client disconnects
    print(f'Client {ws.remote_address} disconnected')

server.serve() # serve forever

Client

Here is a example of websocket client:

from simplews import SimpleWSClient
client = SimpleWSClient(uri="ws://localhost:8765",
                        print_details=True, # Print basic logs
                        print_messages=True) # Print every message received

@client.on_connect
async def on_connect(ws): # called when the connection is established
    print(f'Connected to {ws.remote_address}')

@client.on_message
async def on_message(ws, message): # called when a message is received
    print(f'Received: {message}')

@client.on_close
async def on_close(ws): # called when the connection is closed
    print(f'Connection to {ws.remote_address} closed')

client.connect() # connect to the server

Tips

  • All callbacks are asynchronous, so don't forget async.
  • ws.send() is a coroutine, so don't forget await.
  • For more information about the ws argument, see WebSocketServerProtocol for server and WebSocketClientProtocol for client.
  • server.broadcast() is the preferred way to send a message to all connected clients, and it's not a coroutine, so no await is needed.

License

simple-websockets is distributed under the terms of the GPL-3.0-only license.

FAQs


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