• Stars
    star
    207
  • Rank 189,769 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created about 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket

GoDoc

Websocket client for Centrifugo server and Centrifuge library.

There is no v1 release of this library yet – API still evolves. At the moment patch version updates only contain backwards compatible changes, minor version updates can have backwards incompatible API changes.

Check out client SDK API specification to learn how this SDK behaves. It's recommended to read that before starting to work with this SDK as the spec covers common SDK behavior - describes client and subscription state transitions, main options and methods. Also check out examples folder.

The features implemented by this SDK can be found in SDK feature matrix.

The latest centrifuge-go is compatible only with the latest Centrifugo server (v4) and Centrifuge >= 0.25.0. For Centrifugo v2, Centrifugo v3 and Centrifuge < 0.25.0 you should use centrifuge-go v0.8.x.

Callbacks should not block

When using this SDK you should not block for a long time inside event handlers since handlers called synchronously by the SDK and block the connection read loop. The fact that the read loop is blocked also means that you can not issue blocking Client requests such as Publish, RPC, History, Presence, PresenceStats from the event handler code – this will result into a deadlock. Use a separate goroutine if you really need to issue a blocking call from inside an event handler.

I.e. this code is broken:

client.OnMessage(func(e centrifuge.MessageEvent) {
    result, err := c.RPC(context.Background(), "method", []byte("{}"))
    if err != nil {
        log.Println(err)
        return
    }
    // Will never be called.
    log.Printf("RPC result: %s", string(result.Data))
})

This code is correct as it does not block event handler forever:

client.OnMessage(func(e centrifuge.MessageEvent) {
    // When issue blocking requests from inside event handler we must use
    // a goroutine. Otherwise, we will ge a deadlock since the connection
    // read loop is blocked.
    go func() {
        result, err := c.RPC(context.Background(), "method", []byte("{}"))
        if err != nil {
            log.Println(err)
            return
        }
        log.Printf("RPC result: %s", string(result.Data))
    }()
})

You can find similar limitations in eclipse/paho.mqtt.golang. In short, this is caused by a challenging mix of asynchronous protocol, Go and callback approach. In the previous versions of this SDK we allowed blocking requests from within event handlers – but it contradicts with the real-time nature of Centrifugal ecosystem, because we had to use separate callback queue, and that queue could grow huge without a reasonable way to backpressure (which we were not able to find).

If you are calling Publish, RPC, History, Presence, PresenceStats from the outside of event handler – you should not do any special care. Also, if you are calling your own blocking APIs from inside Centrifuge event handlers – you won't get the deadlock, but the read loop of the underlying connection will not proceed till the event handler returns.

Run tests

First run Centrifugo instance:

docker run -d -p 8000:8000 centrifugo/centrifugo:latest centrifugo --client_insecure

Then run go test

More Repositories

1

centrifugo

Scalable real-time messaging server in a language-agnostic way. Self-hosted alternative to Pubnub, Pusher, Ably. Set up once and forever.
Go
7,470
star
2

centrifuge

Real-time messaging library for Go with scalability in mind. The core of Centrifugo server.
Go
838
star
3

centrifuge-js

JavaScript client SDK to communicate with Centrifugo and Centrifuge-based server from browser, NodeJS and React Native. Supports WebSocket, HTTP-streaming over Fetch and Readable Stream API, EventSource, WebTransport and SockJS.
TypeScript
353
star
4

phpcent

PHP library to communicate with Centrifugo HTTP API
PHP
144
star
5

centrifuge-dart

Dart (Flutter) client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
Dart
89
star
6

gocent

Go library to communicate with Centrifugo HTTP API
Go
75
star
7

pycent

Python library to communicate with Centrifugo HTTP API
Python
63
star
8

centrifuge-mobile

iOS and Android clients for Centrifugo and Centrifuge library using gomobile on top of centrifuge-go
Go
56
star
9

centrifuge-java

General Java and Android client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
Java
56
star
10

centrifuge-android

Android client to communicate with Centrifugo v1 over Websockets (not maintained anymore)
Java
48
star
11

examples

Collection of examples for Centrifugo stack https://centrifugal.dev
PHP
46
star
12

centrifuge-swift

Swift client SDK for bidirectional real-time communication with Centrifugo and Centrifuge-based server over WebSocket
Swift
43
star
13

web

Admin web interface for Centrifugo real-time messaging server
TypeScript
35
star
14

adjacent

Centrifugo v1 integration with Django framework, for Centrifugo v2 use cent Python client to integrate
Python
32
star
15

centrifuge-ios

Swift client to communicate with Centrifugo v1 from iOS over WebSocket (not maintained anymore)
Swift
28
star
16

centrifuge-python

Centrifugo real-time WebSocket SDK for Python on top of asyncio
Python
28
star
17

helm-charts

Official Centrifugo Helm chart for Kubernetes
Mustache
27
star
18

rubycent

Ruby gem to communicate with Centrifugo HTTP API
Ruby
19
star
19

documentation

Centrifugo v1 documentation
CSS
14
star
20

jscent

Node.js client to interact with Centrifugo v1 HTTP API
JavaScript
11
star
21

protocol

Centrifuge client-server protocol definitions
Go
7
star
22

rotor

Rotor is a high-performance PUB/SUB Broker and Presence Manager for Centrifuge and Centrifugo based on Tarantool Cartridge (EXPERIMENTAL)
Lua
7
star
23

centrifugal.dev

Documentation site for Centrifugo
JavaScript
7
star
24

centrifugo-pro

A home for Centrifugo PRO releases
3
star
25

tarantool-centrifuge

Base Lua module to build Centrifugo Tarantool Engine. Work in progress.
Lua
3
star
26

homebrew-centrifugo

Homebrew formula for Centrifugo
Ruby
2
star