MCPcopy Index your code
hub / github.com/djherbis/socket

github.com/djherbis/socket @v0.9.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.9.1 ↗ · + Follow
105 symbols 215 edges 14 files 28 documented · 27%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

socket

GoDoc Software License

Simple Socket.io alternative with #golang server

Usage

Server:

package main

import (
  "fmt"
  "net/http"

  "github.com/djherbis/socket"
)

type MyObject struct{
  Text string `json:"text"`
}

func main() {
  server := socket.NewServer()

  server.On(socket.Connection, func(so socket.Socket) {
    so.Emit("hello", "world")

    so.On("hey", func(msg string) {
      fmt.Println(msg)
    })

    so.On("new obj", func(myobj MyObject){
      fmt.Println(myobj)
    })

    so.Emit("server obj", MyObject{Text: "obj from server to client"})
  })

  router := http.NewServeMux()
  router.Handle("/socket", server)
  router.Handle("/", http.FileServer(http.Dir("."))) // serve up socket.js
  http.ListenAndServe("localhost:8080", router)
}

Client:

<script src="https://github.com/djherbis/socket/raw/v0.9.1/socket.js"></script>
<script>
  var socket = io("localhost:8080/");

  socket.emit("hey", "hey there!");

  socket.on("hello", function(msg){
    console.log(msg);
  });

  socket.emit("new obj", {text: "objects are automatically marshalled/unmarshalled"});

  socket.on("server obj", function(myobj){
    console.log(myobj);
  });
</script>

Installation

# Server Side
go get github.com/djherbis/socket

# Client Side
<script src="https://github.com/djherbis/socket/raw/v0.9.1/socket.js"></script>

Extension points exported contracts — how you extend this code

EventHandler (Interface)
EventHandler registers a function to be run when an event is received. The arguments to the function will be unmarshalle [3 …
handler.go
Emitter (Interface)
Emitter handles sending messages. Emit sends an event and args which will be handled by the func registered via socket.o [3 …
socket.go
Packet (Interface)
Packet is used to receive a sent event and decode the javascript objects into a functions arguments [1 implementers]
transport.go
Room (Interface)
Room is a collection of Sockets [1 implementers]
room.go
Namespace (Interface)
Namespace is used to multiplex a single Transport and allow independent sockets within a single connection. [1 implementers]
namespace.go
ClientSocket (Interface)
ClientSocket creates a client-side Socket
client.go
PacketHandler (Interface)
PacketHandler responds to a Packet [2 implementers]
handler.go
Socket (Interface)
Socket is a Server-side socket [1 implementers]
socket.go

Core symbols most depended-on inside this repo

On
called by 13
handler.go
ID
called by 10
client.go
Emit
called by 8
socket.go
OnPacket
called by 4
handler.go
Join
called by 4
room.go
To
called by 4
socket.go
ns
called by 3
server.go
newRoom
called by 3
room.go

Shape

Method 67
Function 16
Struct 12
Interface 10

Languages

Go100%

Modules by API surface

transport.go24 symbols
socket.go16 symbols
room.go13 symbols
client.go13 symbols
namespace.go11 symbols
handler.go9 symbols
server.go5 symbols
rooms.go5 symbols
caller.go5 symbols
example/main.go2 symbols
example/hook/server.go1 symbols
example/hook/client.go1 symbols

For agents

$ claude mcp add socket \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact