Skip to content

epikoder/nsocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Event driven Messaging broker using native WebSocket

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages