go-socket.io is library an implementation of Socket.IO in Golang

Related tags

go-socket.io
Overview

go-socket.io

GoDoc Build Status Go Report Card

go-socket.io is library an implementation of Socket.IO in Golang, which is a realtime application framework.

Current this library supports 1.4 version of the Socket.IO client. It supports room, namespaces and broadcast at now.

Help wanted This project is looking for contributors to help fix bugs and implement new features. Please check Issue 192. All help is much appreciated.

Contents

Install

Install the package with:

go get github.com/googollee/go-socket.io

Import it with:

import "github.com/googollee/go-socket.io"

and use socketio as the package name inside the code.

Example

Please check more examples into folder in project for details. Examples

FAQ

It is some popular questions about this repository:

  • Is this library supported socket.io version 2?
    • No, but if you wanna you can help to do it. Join us in community chat Telegram
  • How to use go-socket.io with CORS?
  • What is minimal version Golang support for this library?
    • We required Go 1.9 or upper!
  • How to user?
    • Go-socket.io compatibility with Socket.IO 0.9.x, please use branch 0.9.x * or tag [email protected]

Community

Telegram chat: @go_socketio

Engineio

This project contains a sub-package called engineio. This used to be a separate package under https://github.com/googollee/go-engine.io.

It contains the engine.io analog implementation of the original node-package. https://github.com/socketio/engine.io It can be used without the socket.io-implementation. Please check the README.md in engineio/.

License

The 3-clause BSD License - see LICENSE for more details

Issues
  • Client doesn't work with Node.js socket.io server

    Client doesn't work with Node.js socket.io server

    Using the socket.io client example, run against the node.js socket.io server example doesn't work. It appears as :

    panic: invalid status: 400 Bad Request
    
    goroutine 1 [running]:
    runtime.panic(0x642720, 0xc21000a820)
            /usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6
    main.main()
            /home/david/Development/gowork/src/socketclient.go:25 +0x74
    
    goroutine 4 [syscall]:
    runtime.goexit()
            /usr/lib/go/src/pkg/runtime/proc.c:1394
    
    goroutine 5 [runnable]:
    net/http.(*persistConn).readLoop(0xc21005b400)
            /usr/lib/go/src/pkg/net/http/transport.go:778 +0x68f
    created by net/http.(*Transport).dialConn
            /usr/lib/go/src/pkg/net/http/transport.go:528 +0x607
    
    goroutine 6 [select]:
    net/http.(*persistConn).writeLoop(0xc21005b400)
            /usr/lib/go/src/pkg/net/http/transport.go:791 +0x271
    created by net/http.(*Transport).dialConn
            /usr/lib/go/src/pkg/net/http/transport.go:529 +0x61e
    

    The server returns a 400 error with the response message 'transport:undefined'

    The equivalent node.js client seems to connect to the socket.io url with an additional transport param set to 'polling', and then goes through a protocol upgrade to websockets.

    Is a node server/ go client expected to work?

    question 
    opened by daviddawson 24
  • disconnection event has not emit

    disconnection event has not emit

    use the demo file main.go refresh the browser,the log is:

    2014/08/17 17:40:03 main.go:17: on connection
    2014/08/17 17:40:06 main.go:17: on connection
    2014/08/17 17:40:07 main.go:17: on connection
    2014/08/17 17:40:09 main.go:17: on connection
    
    opened by hidu 23
  • example in documentation  report

    example in documentation report "http: multiple response.WriteHeader calls" error when client connect from browser. And the client received "Error during WebSocket handshake Ask Unexpected response code: 403"

    Hi, guys, I tried your example in branch 1.4, i write a simplest web page to conncet the example main.go server, first i received cors error, after fix that , then the server print out some log "http: multiple response.WriteHeader calls" and the client also report the error : "Error during WebSocket handshake Ask Unexpected response code: 403", do someone see this problem before?

    question documentation might-be-cors-issue 
    opened by mmmmmagina 23
  • Writing data more frequently will cause panic

    Writing data more frequently will cause panic

    发现如果数据发送的比较频繁,会导致与ping的数据写时并发,go race 在大量的客户端的情况下,会引起websocket的写并发panic。

    bug 
    opened by pangdahua 20
  • http: response.WriteHeader on hijacked connection using websocket transport

    http: response.WriteHeader on hijacked connection using websocket transport

    Hi! Thanks for a library. I have faced a strange problem using it and cannot fix it by myself

    Here is a simple reproduce case:

    package main
    
    import (
        "log"
        "net/http"
    
        "github.com/googollee/go-socket.io"
    )
    
    func main() {
        server, err := socketio.NewServer(nil)
        if err != nil {
            log.Fatal(err)
        }
        server.On("connection", func(so socketio.Socket) {
            log.Println("on connection")
            so.Join("chat")
            so.On("chat message", func(msg string) {
                log.Println("emit:", so.Emit("chat message", msg))
                so.BroadcastTo("chat", "chat message", msg)
            })
            so.On("disconnection", func() {
                log.Println("on disconnect")
            })
        })
        server.On("error", func(so socketio.Socket, err error) {
            log.Println("error:", err)
        })
    
        http.Handle("/socket.io/", server)
        http.Handle("/", http.FileServer(http.Dir("./asset")))
        log.Println("Serving at localhost:5000...")
        log.Fatal(http.ListenAndServe(":5000", nil))
    }
    

    and in the browser I use something like

    <!doctype html>
    <html>
    <head>
        <title>Socket.IO chat</title>
    </head>
    <body>
    <script src="socket.io.js"></script>
    <script>
        var socket = io('localhost:5000', {transports: ["websocket"]})
        console.log(socket)
    </script>
    </body>
    </html>
    

    Then I see strange warnings in my applications's log:

    2015/11/21 20:43:30 Serving at localhost:5000...
    2015/11/21 20:43:35 http: response.WriteHeader on hijacked connection
    2015/11/21 20:43:35 on connection
    

    Using gdb I have found that the problem is that we are still serving http on hijacked connection.

    2015/11/21 20:46:17 Serving at localhost:5000...
    
    Breakpoint 1, net/http.(*response).WriteHeader (w=0xc8200b3810, code=400) at /usr/lib/go/src/net/http/server.go:683
    683         w.conn.server.logf("http: response.WriteHeader on hijacked connection")
    (gdb) bt
    #0  net/http.(*response).WriteHeader (w=0xc8200b3810, code=400) at /usr/lib/go/src/net/http/server.go:683
    #1  0x000000000065db00 in github.com/googollee/go-engine.io/websocket.(*Server).ServeHTTP (s=0xc82000e960, w=..., 
        r=0xc8200d2000) at /root/gopath/src/github.com/googollee/go-engine.io/websocket/server.go:35
    #2  0x00000000005d8cb9 in github.com/googollee/go-engine%2eio.(*serverConn).ServeHTTP (c=0xc820057d40, w=..., r=0xc8200d2000)
        at /root/gopath/src/github.com/googollee/go-engine.io/server_conn.go:197
    #3  0x00000000005d717d in github.com/googollee/go-engine%2eio.(*Server).ServeHTTP (s=0xc8200160e0, w=..., r=0xc8200d2000)
        at /root/gopath/src/github.com/googollee/go-engine.io/server.go:159
    #4  0x00000000004a5783 in github.com/googollee/go-socket%2eio.(*Server).ServeHTTP (s=0xc82000e660, w=..., r=0xc8200d2000)
        at /root/gopath/src/github.com/googollee/go-socket.io/server.go:87
    #5  0x000000000047d16d in net/http.(*ServeMux).ServeHTTP (mux=0xc820010960, w=..., r=0xc8200d2000)
        at /usr/lib/go/src/net/http/server.go:1699
    #6  0x000000000047dbde in net/http.serverHandler.ServeHTTP (sh=..., rw=..., req=0xc8200d2000)
        at /usr/lib/go/src/net/http/server.go:1862
    #7  0x000000000047b3ce in net/http.(*conn).serve (c=0xc8200b3550) at /usr/lib/go/src/net/http/server.go:1361
    #8  0x000000000045bd01 in runtime.goexit () at /usr/lib/go/src/runtime/asm_amd64.s:1696
    #9  0x000000c8200b3550 in ?? ()
    #10 0x0000000000000000 in ?? ()
    

    But I am not familiar with websockets and your implementation to go further.

    Is it my mistake somewhere or bug in library? Thank you

    opened by bo0rsh201 16
  • support cluster?

    support cluster?

    can support cluster?

    duplicate 
    opened by arden 16
  • Merge engine.io into socket.io

    Merge engine.io into socket.io

    Discussion at #296

    v2 
    opened by adrianmxb 13
  • Client API

    Client API

    Currently there is only the Server API. Are there any plans to add a client as well?

    opened by fd 13
  • I'm getting bad request

    I'm getting bad request

    When emitting events from client, I got Bad Request

    POST http://localhost:8500/socket.io/?EIO=3&transport=polling&t=1578039627529-2&sid=4 400 (Bad Request)

    And on the server console, it said 'resume'

    Here's my client code:

    var socket = io();
    
    var mesageForm = document.getElementById('send-container');
    var mesageContainer = document.getElementById('message-container');
    var mesageInput = document.getElementById('message-input');
    
    
    const name = prompt('What is your name?');
    appendMessage('You' + ' joined');
    
    socket.emit('new-user', name);
    
    socket.on('chat-message', data => {
        appendMessage(data)
    });
    
    socket.on('user-connected', name => {
        appendMessage(name + ' joined')
    });
    
    mesageForm.addEventListener('submit', function (e) {
        e.preventDefault();
    
        const message = mesageInput.value;
        socket.emit('send-chat-message', message);
        mesageInput.value = ''
    });
    
    function appendMessage(msg) {
        const messageElement = document.createElement('div');
    
        messageElement.innerText = msg;
        mesageContainer.append(messageElement)
    }
    

    And here's my server code

    package main
    
    import (
    	socketio "github.com/googollee/go-socket.io"
    	"github.com/labstack/echo"
    )
    
    type Sock struct {
    	sock socketio.Conn
    	Name string
    }
    
    var socks = make(map[string]Sock)
    
    func main() {
    	io, _ := socketio.NewServer(nil)
    	
    	app := echo.New()
    	
    	app.Static("/", ".")
    	
    	app.Any("/socket.io/", func(s echo.Context) error {
    		io.ServeHTTP(s.Response().Writer, s.Request())
    		
    		return nil
    	})
    	
    	io.OnConnect("/", func(s socketio.Conn) error {
    		socks[s.ID()] = Sock{
    			sock: s,
    		}
    		
    		s.Emit("chat-message", "Server: Hello World")
    		
    		return nil
    	})
    	
    	io.OnEvent("/", "new-user", func(s socketio.Conn, name string) {
    		sk := socks[s.ID()]
    		sk.Name = name
    		
    		for k, v := range socks {
    			if k != s.ID() {
    				v.sock.Emit("user-connected", name)
    			}
    		}
    	})
    	
    	io.OnEvent("/", "send-chat-message", func(s socketio.Conn, msg string) {
    		for k, v := range socks {
    			if k != s.ID() {
    				v.sock.Emit("chat-message", msg)
    			}
    		}
    	})
    	
    	go io.Serve()
    	defer io.Close()
    	
    	app.Start(":8500")
    }
    

    It's annoying. What is causing this error?

    might-be-cors-issue 
    opened by cyantarek 13
  • I getting unexpected and unexplained message while running the socket

    I getting unexpected and unexplained message while running the socket

    Describe the bug I'm getting the bellow message and not sure why its coming and how to solve it "Did you forget to Close() the ReadCloser from NextReader?"

    To Reproduce Run a socket connection with multiple clients which keep sending message

    Expected behavior should explain where this problem is happening and logs or point towards how to solve it

    Environment (please complete the following information):

    • Go version: 1.13
    • Server version v1.4.3
    • Client version v2.3.0

    Additional context "Did you forget to Close() the ReadCloser from NextReader?"

    bug 
    opened by Albinzr 12
  • How to solve 403 error while using go-gin

    How to solve 403 error while using go-gin

    Hello I'm facing issues with gin. I tried using CORS middleware, but it shows 403 error here is my router and server := socketio.NewServer(nil)

    h.SocketService := *server

    router.GET("/socket.io/*any", gin.WrapH(h.SocketService)) router.POST("/socket.io/*any", gin.WrapH(h.SocketService))

    opened by rajatvyadav 1
  • socket client

    socket client

    hi guys i saw it supports scoket client 1.4 which is the version extraheaders were added but it is not working i am using the latest verion of go_websocket.io 1.6?

    opened by LibenHailu 1
  • Decoder for the polling transport.

    Decoder for the polling transport.

    null

    opened by googollee 0
  • how to test go_socket???

    how to test go_socket???

    testing sample please?? i dont know how i to test it??

    opened by LibenHailu 0
  • How to connect redis adaptor for test local server send message and cloud server can get the message?

    How to connect redis adaptor for test local server send message and cloud server can get the message?

    serverredisconfig I'm config redis adaptor for test about localhost send message across to server connect testsendmessageredis I'm test send message on the localhost but on the server isn't get a message. How to solve it? 1629213806834@2x I'm read the socket io document.It can send

    question 
    opened by surapong-thom 0
  • getting request for authentication?

    getting request for authentication?

    how can i get the original request handshake in tag v.1.6 socketio.conn.RemoteHeader() sends only the header but i want the hole handshake request response l a response like socket.Request() in 1.4.0?

    question 
    opened by LibenHailu 1
  • Socket IO Client Example with Namespace

    Socket IO Client Example with Namespace

    Hi Folks! Thanks very much for maintaining the library.

    Wanted to check if there's an example for Client Connection with Namespace?

    @ambelovsky @googollee

    opened by BB-falconX 0
  • root.onError(nil, err) Why use nil

    root.onError(nil, err) Why use nil

    func (s *Server) serveConn(conn engineio.Conn) { err := newConn(conn, s.handlers)

    if err != nil {
    	root, _ := s.handlers.Get(rootNamespace)
    	if root != nil && root.onError != nil {
    		root.onError(nil, err)
    	}
    }
    

    }

    Can't know which connection error, what do you want to do for subsequent processing

    question 
    opened by jinsuojinsuo 0
  • can support redis auth?

    can support redis auth?

    I wan't to use redis broadcast,but the redis set password. can support redis auth?

    question 
    opened by zengbin3013 2
Releases(v1.6.1)
Owner
Googol Lee
Googol Lee
Tiny WebSocket library for Go.

RFC6455 WebSocket implementation in Go.

Sergey Kamardin 4.1k Sep 22, 2021
A fast, well-tested and widely used WebSocket implementation for Go.

Gorilla WebSocket Gorilla WebSocket is a Go implementation of the WebSocket protocol. Documentation API Reference Chat example Command example Client

Gorilla Web Toolkit 15.5k Sep 17, 2021
Minimal and idiomatic WebSocket library for Go

websocket websocket is a minimal and idiomatic WebSocket library for Go. Install go get nhooyr.io/websocket Highlights Minimal and idiomatic API First

Anmol Sethi 1.9k Sep 17, 2021
Chat bots (& more) for Zoom by figuring out their websocket protocol

zoomer - Bot library for Zoom meetings Good bot support is part of what makes Discord so nice to use. Unfortunately, the official Zoom API is basicall

Christopher Tarry 41 Sep 2, 2021
proxy your traffic through CDN using websocket

go-cdn2proxy proxy your traffic through CDN using websocket what does it do example server client thanks what does it do you can use this as a library

jm33-ng 31 Sep 9, 2021
WebSocket for fasthttp

websocket WebSocket library for fasthttp and net/http. Checkout examples to inspire yourself. Install go get github.com/dgrr/websocket Why another Web

Darío 15 Sep 20, 2021
:notes: Minimalist websocket framework for Go

melody ?? Minimalist websocket framework for Go. Melody is websocket framework based on github.com/gorilla/websocket that abstracts away the tedious p

Ola 2.2k Sep 20, 2021
A modern, fast and scalable websocket framework with elegant API written in Go

About neffos Neffos is a cross-platform real-time framework with expressive, elegant API written in Go. Neffos takes the pain out of development by ea

Gerasimos (Makis) Maropoulos 367 Sep 16, 2021
websocket消息推送服务

balloons-websocket 用于构建实时应用程序的基础架构和API,balloons提供了最好的基础架构和API,以大规模地提供实时体验。向最终用户提供快速稳定的实时消息。让我们处理实时消息传递的复杂性,以便您可以专注于代码。 balloons的实时API向开发人员公开了整个balloon

null 11 Apr 4, 2021
gatews - Gate.io WebSocket SDK

gatews - Gate.io WebSocket SDK gatews provides new Gate.io WebSocket V4 implementations. It is intended to work along with gateapi-* series to provide

gate.io 19 Aug 29, 2021