Categorygithub.com/epikoder/nsocket
modulepackage
1.0.6
Repository: https://github.com/epikoder/nsocket.git
Documentation: pkg.go.dev

# README

NSocket

Messaging framework with channels [rooms]

Inspired by Socket.io and Kataras Neffos

Installation

go get github.com/epikoder/nsocket

Usage

events := nsocket.Event{
    "/": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
     fmt.Printf("Namespace: [Default] -- GOT: %v ---- from ----  %v\n", i, s.RemoteAddr())
     if err := soc.Broadcast("namespace:default -- " + fmt.Sprintf("%v ------> %v", i, s.RemoteAddr())); err != nil {
      t.Error(err)
     }
    },
    "message": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
     fmt.Printf("Namespace: [Default/Message] -- GOT: %v ---- from ----  %v\n", i, s.RemoteAddr())
     if err := soc.Emit("namespace:message -- "+fmt.Sprintf("%v ------> %v", i, s.RemoteAddr()), "message"); err != nil {
      t.Error(err)
     }
    },
   }
    
soc := nsocket.New(nsocket.Config{
  AuthFunc: func(r *http.Request) (ok bool) { // Optional: Add authentication : On https Websocket send cookie to the server
   c, err := r.Cookie("auth")
   if err != nil {
    return
   }
   return c.Value == authKey
  },
  AllowedOrigins: []string{"localhost:3000", "localhost:8000"}, //Optional: Set allowed origins if needed
  Namespace: nsocket.Namespace{
   nsocket.Default: events // Events functions are called when message is received from the client
  },
 })

 mux := http.NewServeMux()
 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  if err := soc.Serve(w, r); err != nil {
   panic(err)
  }
 })

 fmt.Println("starting server on: http://localhost:8000")
 http.ListenAndServe(":8000", mux) // Start websocket server

Note

This library is still under development and not production ready, use with caution.

# Functions

No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author