MCPcopy Create free account
hub / github.com/CPChain/chain / Subscribe

Method Subscribe

api/rpc/client.go:415–447  ·  view source on GitHub ↗

Subscribe calls the " _subscribe" method with the given arguments, registering a subscription. Server notifications for the subscription are sent to the given channel. The element type of the channel must match the expected type of content returned by the subscription. The context argumen

(ctx context.Context, namespace string, channel interface{}, args ...interface{})

Source from the content-addressed store, hash-verified

413// ErrSubscriptionQueueOverflow. Use a sufficiently large buffer on the channel or ensure
414// that the channel usually has at least one reader to prevent this issue.
415func (c *Client) Subscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) (*ClientSubscription, error) {
416 // Check type of channel first.
417 chanVal := reflect.ValueOf(channel)
418 if chanVal.Kind() != reflect.Chan || chanVal.Type().ChanDir()&reflect.SendDir == 0 {
419 panic("first argument to Subscribe must be a writable channel")
420 }
421 if chanVal.IsNil() {
422 panic("channel given to Subscribe must not be nil")
423 }
424 if c.isHTTP {
425 return nil, ErrNotificationsUnsupported
426 }
427
428 msg, err := c.newMessage(namespace+subscribeMethodSuffix, args...)
429 if err != nil {
430 return nil, err
431 }
432 op := &requestOp{
433 ids: []json.RawMessage{msg.ID},
434 resp: make(chan *jsonrpcMessage),
435 sub: newClientSubscription(c, namespace, chanVal),
436 }
437
438 // Send the subscription request.
439 // The arrival and validity of the response is signaled on sub.quit.
440 if err := c.send(ctx, op, msg); err != nil {
441 return nil, err
442 }
443 if _, err := op.wait(ctx); err != nil {
444 return nil, err
445 }
446 return op.sub, nil
447}
448
449func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMessage, error) {
450 params, err := json.Marshal(paramsIn)

Callers 2

EthSubscribeMethod · 0.95
ShhSubscribeMethod · 0.95

Calls 5

newMessageMethod · 0.95
sendMethod · 0.95
waitMethod · 0.95
newClientSubscriptionFunction · 0.85
TypeMethod · 0.80

Tested by

no test coverage detected