5

So I was using the web3 API from the NodeJS 'web3' module very happily and it works very well (it uses HTTP transport AFAIU). But now I want to receive events from the server (without polling), and for that it seems I need to use the subscription API.

However, the documentation I just linked states subscriptions require a full duplex connection. Geth offers such connections in the form of websockets (enable with --ws) and ipc (enabled by default). Fair enough.

But, for the life of me, I cannot find any sample out there of how to connect via IPC/WebSockets. If the documentation included a simple hello world sample of a subscription, it would be rad.

1

3 Answers 3

1

you can also use netcat :

➜  ~  nc -U /home/XXX/.ethereum/geth.ipc
{"jsonrpc":"2.0","method":"eth_coinbase","params":[],"id":1}
{"jsonrpc":"2.0","id":1,"result":"0xeeeeeeeeeeeeeeeeeeeeededededede"}
3
  • how about using nodejs? :) (if you provide js sample that I'll upvote all your 3 answers :) )
    – knocte
    May 19, 2016 at 8:23
  • i'm sorry I don't know js libraries well enough for that but I'm pretty sure other will find a way ;)
    – euri10
    May 19, 2016 at 8:44
  • @knocte, bribing him? Did you find js sample? if you send me I'll upvote your answers ;) Mar 6, 2018 at 15:00
1

hacking around I connected in python this way, pretty simple:

➜  ~ ipython2
Python 2.7.11+ (default, May  9 2016, 15:54:33)
Type "copyright", "credits" or "license" for more information.

IPython 4.2.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import socket

In [3]: s=socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)

In [4]: s.connect("/home/XXX/.ethereum/geth.ipc")

In [5]: message = '{"jsonrpc":"2.0","method":"eth_coinbase","params":[],"id":1}'

In [6]: s.send(message)
Out[6]: 60

In [7]: s.recv(1024)
Out[7]: '{"jsonrpc":"2.0","id":1,"result":"0xeeeeeeeeeeeeeeeeeeeeededededede"}\n'

still browsing it seems this library is designed to do exactly what you want, but in python

1

web3 has providers for IPC and Websockets

http://web3js.readthedocs.io/en/1.0/web3.html#providers

You can use it simply with

const web3 = new Web3(new Web3.providers.IpcProvider(/path/to/.ipc, net))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.