---
title: "Bidirectional Connections"
url: https://docs.zeroc.com/ice/3.8/runtime/connection-management/bidirectional-connections
language: js
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Bidirectional Connections

Programming language: JavaScript. Other languages: [C++](https://docs.zeroc.com/ice/3.8/runtime/connection-management/bidirectional-connections/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/connection-management/bidirectional-connections/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/connection-management/bidirectional-connections/java.md), [Python](https://docs.zeroc.com/ice/3.8/runtime/connection-management/bidirectional-connections/python.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/connection-management/bidirectional-connections/swift.md).

## Use Cases for Bidirectional Connections

By default, requests flow over an Ice connection in one direction: from the client that establishes the connection to the server that accepts it. If the server needs to call back the client, it usually establishes a new connection to that client to send these callback requests, as shown below:

![The client opens a connection to send requests to the server. The server opens a separate connection to send callbacks to the client.](https://docs.zeroc.com/images/ice/3.8/bidirectional-connections/callback1.svg)

*Callbacks in an open network.*

A firewall in front of the client, however, typically blocks this connection:

![The client can open a connection to the server through its firewall, but the firewall blocks a separate incoming callback connection from the server.](https://docs.zeroc.com/images/ice/3.8/bidirectional-connections/callback2.svg)

A bidirectional connection solves this: requests flow over it in both directions, so the server can send callbacks over the connection the client established. This page calls the side that opened the connection the client and the side that accepted it the server, including when the server sends a callback and is therefore the [client](https://docs.zeroc.com/ice/3.8/basics/terminology.md#clients-and-servers) of this request.

## Configuring a Client for Bidirectional Connections

A client needs to perform the following steps in order to configure a bidirectional connection:

1. [Create an object adapter](https://docs.zeroc.com/ice/3.8/runtime/dispatch/creating-an-object-adapter/js.md) to receive callback requests.
2. Set this object adapter as the default object adapter on the communicator. The communicator associates its default object adapter with the outgoing connections it creates afterward.
3. [Register the callback object](https://docs.zeroc.com/ice/3.8/runtime/dispatch/object-adapter-activation-and-deactivation.md) or objects with the object adapter.

> **Tip**
>
> We recommend a dedicated object adapter for the callbacks. Such an object adapter needs no name or endpoints, and you don't need to activate it, since it accepts no connection: it only dispatches the callbacks received over the bidirectional connections. You can also use an object adapter with endpoints: it then dispatches the requests received over its own incoming connections as well as the callbacks.

The code below illustrates these steps:

```typescript
const adapter = await communicator.createObjectAdapter("");
communicator.setDefaultObjectAdapter(adapter);
const mockAlarmClock = new MockAlarmClock();
adapter.add(mockAlarmClock, new Ice.Identity("alarmClock"));
```

The callback object (`mockAlarmClock` in the code above) will handle incoming requests for identity `alarmClock`.

To set an object adapter on a connection created before you set the default object adapter, or to give a connection a different object adapter, call `setAdapter` on this outgoing connection, for example the connection returned by a proxy's `ice_getConnection`. A connection without an object adapter rejects incoming requests with `ObjectNotExistException`.

These two approaches differ in scope. With a default object adapter, every outgoing connection that the communicator creates afterward dispatches the requests it receives to this adapter, so any server the client connects to can send callbacks to it. With `setAdapter`, only the connections you choose accept callbacks.

## Configuring a Server for Bidirectional Connections

> **Note**
>
> Ice for JavaScript cannot accept incoming connections, so a JavaScript application is always the client of a bidirectional connection: it establishes the connection and receives callbacks over it, as described in [Configuring a Client for Bidirectional Connections](#configuring-a-client-for-bidirectional-connections).

The [Ice bidir demo](https://github.com/zeroc-ice/ice-demos/tree/3.8/js/Ice/bidir) provides a complete JavaScript client that receives callbacks over a bidirectional connection, from a bidir demo server written in another language.

## Limitations of Bidirectional Connections

Bidirectional connections have certain limitations:

- They can only be configured for connection-oriented transports such as TCP and SSL.
- A fixed proxy is bound to an existing connection: you cannot change the settings that select a connection, such as its endpoints or its router, and you cannot marshal it or convert it to proxy properties. You can still change its invocation mode, for example to oneway.

## See Also

- [Callbacks through Glacier2](https://docs.zeroc.com/ice/3.8/services/glacier2/callbacks-through-glacier2/js.md)
- [Creating an Object Adapter](https://docs.zeroc.com/ice/3.8/runtime/dispatch/creating-an-object-adapter/js.md)
- [Object Adapter Activation and Deactivation](https://docs.zeroc.com/ice/3.8/runtime/dispatch/object-adapter-activation-and-deactivation.md)
- [Object Identity](https://docs.zeroc.com/ice/3.8/runtime/object-identity/js.md)
- [Nested Invocations](https://docs.zeroc.com/ice/3.8/runtime/threading-model/nested-invocations.md)
- [The Ice Threading Model](https://docs.zeroc.com/ice/3.8/runtime/threading-model.md)
- [Object Adapter Thread Pools](https://docs.zeroc.com/ice/3.8/runtime/threading-model/object-adapter-thread-pools.md)
