GithubHelp home page GithubHelp logo

fpablos / gosocketio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pragmaticivan/gosocketio

0.0 1.0 0.0 143 KB

Go socket.io client forked from https://github.com/wedeploy/gosocketio and modified.

License: MIT License

Makefile 1.84% Go 90.18% JavaScript 5.08% Shell 2.90%

gosocketio's Introduction

golang socket.io

GoDoc Build Status

golang socket.io is an implementation for the socket.io protocol in Go. There is a lack of specification for the socket.io protocol, so reverse engineering is the easiest way to find out how it works.

This is a work in progress. Many features, such as middleware and binary support, are missing.

golang socket.io is an adapted work from github.com/graarh/golang-socketio.


on "connection", "error", and "disconnection"

socket.io has three special events it triggers on the client-side and you should not emit them on your own programs.

Wait for the socket.io connection event before emitting messages or you risk losing them due in an unpredictable fashion (due to concurrency: connection latency, server load, etc.). For the default namespace this is automatically handled on gosocketio.Connect.

However, before emitting a message on a custom namespace, you want to wait for the ready signal, like so:

ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()

select {
	case <-ctx.Done():
		return ctx.Err()
	case <-exampleNamespace.Ready():
		// don't need to do anything
}

if err := exampleNamespace.Emit("fleet", 100); err != nil {
	return err
}

The reason why you probably want to use a select receiving a second channel, such as context.Done() on all non-trivial programs is to avoid program loop, leak memory, or both in case of failure.

The default namespace is automatically ready after establishing the socket.io session. Therefore, *gosocketio.Client doesn't expose a Ready() method.

Connecting to a socket.io server with a custom namespace

You can connect to a namespace and start emitting messages to it with:

c, err := gosocketio.ConnectContext(ctx, u, websocket.NewTransport())

if err != nil {
	return err
}

// ...

exampleNamespace, err := c.Of("example")

If err != nil {
	return err
}

<-exampleNamespace.Ready() // don't do this, use a select like shown above instead!

If err := exampleNamespace.Emit("list", "friends"); err != nil {
	return err
}

Running the example

  1. npm install to install the dependencies for the example server
  2. node server.js
  3. go run example.go

If you need to improve this library you should consider using these tools:

This library is used by the WeDeploy Command-Line Interface tool to open a shell and execute commands on Docker container services.

gosocketio's People

Contributors

fpablos avatar graarh avatar henvic avatar penguinpowernz avatar pragmaticivan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.